Questions about php class creation

Source: Internet
Author: User
Index of the new php class. php file function & nbsp ;__ autoload ($ _ className) & nbsp; {require & nbsp; $ _ className .. class. php;} create a class ?? If & nbsp; (isset ($ _ GET [index]) & nbsp; {$ mnew & nbsp; Main ($ _ GET php class new question
Index. php file
Function _ autoload ($ _ className ){
Require $ _ className. '. class. php ';
}
// Create a class ??
If (isset ($ _ GET ['index']) {
$ M = new Main ($ _ GET ['index']);
} Else {
$ M = new Main ();
}
Include $ m-> ui ();

Main. class. php file

Class Main {
Private $ index;
// Constructor to initialize data
Public function _ construct ($ index = ''){
$ This-> index = $ index;
}
// The ui function include the corresponding inclusion file.
Public function ui (){
If (empty ($ this-> index) |! File_exists ($ this-> index. '. Inc ')){
$ This-> index = 'start ';
}
Return $ this-> index. '. Inc ';
}
}
What is the significance of the red part? the parameter value passed by the constructor in the class has been set to null by default (public function _ construct ($ index = '')), why can't I directly write $ m = new Main ($ _ GET ['index']);. If you do not want to make the if judgment in the red letter in the index, you need to write it in the class. Thank you.
------ Solution ----------------------
If (isset ($ _ GET ['index']) {
$ M = new Main ($ _ GET ['index']); // if $ _ GET ['index'] exists, $ _ GET ['index'] is used as the parameter.
} Else {
$ M = new Main (); // otherwise use the default parameter
}

Using $ _ GET ['index'] directly may cause a NOTICE-level error.
Using incoming data without additional partitions may cause security problems.
------ Solution ----------------------
Let's take a look.
 Class Main {

Private $ index;

// Constructor to initialize data
Public function _ construct ($ index = '')
{
$ This-> index = $ index? $ Index :'';
}

// The ui function include the corresponding inclusion file.

Public function ui ()
{
If (empty ($ this-> index)
------ Solution ----------------------
! File_exists ($ this-> index. '. Inc '))
{
$ This-> index = 'start ';
}

Return $ this-> index. '. Inc ';
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.