Please help? Phpoop case

Source: Internet
Author: User
Tags php oop
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;

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.