Questions about _ set ()-php Tutorial

Source: Internet
Author: User
PHPcode & lt ;? Phpnamespacelongli; classEmployee {private $ name; publicfunction _ set ($ xproName, $ value) {echo & lt; p & gt; Employee :: __setiscalled $ pr about _ set ()





PHP code
  
  Employee::__set is called $proName=='.$xproName.', $value=='.$value.'

'; $this->xproName = $value; }}class CEO extends Employee { function __construct($name="") { echo 'CEO::__construct begin!'; $this->name = $name; echo 'CEO::__construct end!'; } }$boss = new CEO("Blus");?>




The above code __set () was inexplicably executed twice. why? Output result:

CEO: __construct begin!

Employee: :__ set is called $ proName = name, $ value = Blus

Employee: :__ set is called $ proName = xproName, $ value = Blus

CEO: __construct end!








------ Solution --------------------
_ Set ($ property, $ value): called when an undefined property is assigned a value.
If you assign a value twice, you can execute it twice.
------ Solution --------------------
The first time an undefined class member $ name is assigned: $ this-> name = $ name; once called
When you call the overload method _ set, assign $ xproName to the undefined class member for the second time: $ this-> xproName = $ value; call once.
I think your intention is as follows:
PHP code
Public function _ set ($ xproName, $ value) {echo'

Employee: :__ set is called $ proName = '. $ xproName.', $ value = '. $ value .'

'; // This $ xproName is a variable whose content is the variable name. For details, see variable $ this-> $ xproName = $ value;} in the manual ;}
------ Solution --------------------
Predefines the two functions "_ get ()" and "_ set ()" to obtain and assign values to their attributes, and checks the attribute "_ isset ()"


// _ Get () is used to obtain private attributes.
Private function _ get ($ property_name)
{
If (isset ($ this-> $ property_name ))
{
Return ($ this-> $ property_name );
} Else
{
Return (NULL );
}
}
// _ Set () is used to set private attributes.
Private function _ set ($ property_name, $ value)
{
$ This-> $ property_name = $ value;
}
------ Solution --------------------
You still haven't figured out the function of _ set (). when you assign a value to the class member attribute $ xproName, it is no longer undefined. Therefore, it does not have the conditions for endless loops.
I think the encapsulation implemented by _ set () is too much encapsulation. why do we need to modify a hidden attribute outside the class? Why is it not set to public?

In fact, to "add a hidden attribute"
Add a hidden property? Isn't the property name required by your program fixed? You should declare this hidden property first.

------ Solution --------------------
Each attribute of an object is configured with an Access and Assign method in the object-oriented language to allow developers to flexibly control the behavior of objects. Because php is not an object-oriented language, although the _ get and _ set methods are rough, there is a big gap between php and Access and Assign.
Like Access and Assign, if _ get and _ set are used only to Access object attributes, it is suspected that they are superfluous.
The functions of _ get and _ set in objects are to implicitly perform some methods while accessing attributes, without explicitly calling methods.

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.