How do we pass parameters when instantiating a php class? This depends on the constructor of the class. How do we pass parameters when instantiating a php class? This depends on the constructor of the class.
Example:
Person. class. php
Class person {
Var $ name;
Var $ color;
Var $ sex;
Var $ age;
Function _ construct ($ name, $ age = '', $ sex = 'boys '){
$ This-> name = $ name;
$ This-> age = $ age;
$ This-> sex = $ sex;
$ This-> color = 'yello ';
}
Function eat (){
Echo $ this-> name. 'Have dinner ';
}
Function xinxi (){
Echo $ this-> name. 'is'. $ this-> sex. 'and age is'. $ this-> age. 'fuse is '. $ this-> color;
}
}
?>
Son. php
Include ('person. class. php ');
$ Son = new person ('cuihua', 25, 'Girl '); // The parameter transfer here corresponds to the parameter order in the class constructor.
// $ Son-> xinxi (); // cuihua is girl and age is 25 fuse is yello
$ Son-> name = 'genniu ';
$ Son-> eat (); // Tian Niu wants to eat
?>