Please help? Phpoop example & lt ;? Php & nbsp; class & nbsp; animal {public & nbsp; $ name = ""; public & nbsp; $ color = ""; public & nbsp; $ age = ""; function & nbsp; ge, please help? Php oop example
Class animal {
Public $ name = "";
Public $ color = "";
Public $ age = "";
Function getInfo () {return $ this-> name ;}
Function setInfo ($ name) {return $ this-> name;} // The compiler prompts a warning.
}
$ Pig = new animal ();
$ Pig-> setInfo ('pi ');
$ Name = $ pig-> getInfo (); // The compiler prompts an error.
Echo $ name;
?>Share with PHP:
------ Solution --------------------
$ Pig = new animal ();
$ Pig-> setInfo ('pipi'); // This line is incorrect. your semicolon is the full-width semicolon.
$ Name = $ pig-> getInfo (); // The compiler prompts an error.
Echo $ name;
/**
In addition, you can format the code later.
*/
------ Solution --------------------
Is the first line a full-width semicolon?
------ Solution --------------------
class person{
private $name;
function setname($name){
return $this->name = $name;
}
function getname(){
return $this->name;
}
}
$p = new person();
$p->setname('lizhi');
echo $p->getname();
?>
------ Solution --------------------
Why is an error reported,
The reason is that you have an upper-case semicolon in line 10th,
Why didn't I output "pig"
Try
Function setInfo ($ name) {return $ this-> name;} // The compiler prompts a warning.
Change
Function setInfo ($ name) {$ this-> name = $ name;} // The compiler prompts an alert.
------ Solution --------------------
Class animal {
Public $ name = "";
Public $ color = "";
Public $ age = "";
Function getInfo (){
Return $ this-> name;
}
Function setInfo ($ name ){
$ This-> name = $ name; // The value should be assigned here. you must have copied it incorrectly.
}
}
$ Pig = new animal ();
$ Pig-> setInfo ('pipi'); // It turns out to be full-width;
$ Name = $ pig-> getInfo ();
Echo $ name;