Newcomer: why cannot I assign values to and output private fields through _ get () and _ set? Solution
Source: Internet
Author: User
Newcomer: why cannot I assign values to and output private fields through _ get () and _ set? Example: & lt; html & gt; & lt; head & gt; & lt; title & gt; & lt;/head & gt; & lt; body & gt; & lt ;? PhpclassEmplo: why cannot I assign values to and output private fields through _ get () and _ set?
Example:
Class Employee {
Private $ name;
Function _ get ($ propName ){
Echo "_ get called!
"; // And cannot output any strings
Return $ this-> $ propName;
}
Function _ set ($ propName, $ propValue ){
$ This-> $ propName = $ propValue;
}
}
$ Employee = new Employee ();
$ Employee-> name = "Mario ";
Echo $ employee-> name ."
";
?>
Why? What parameters do you want to enable?
------ Solution --------------------
Function _ get ($ propName ){
Echo "_ get called!
"; // And cannot output any strings
Return $ this-> $ propName;
}
Isn't it a red underline?
The Magic function name used to implement this function is _ get.
Is two underscores
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