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 ';
}
}