PHP object-oriented improvements page 1/2

Source: Internet
Author: User

First look at the Code:
Copy codeThe Code is as follows:
<? Php
Class StrictCoordinateClass {
Private $ arr = array ('x' => NULL, 'y' => NULL );
Function _ construct ()
{
Print "StrictCoordinateClass is being created ";
Print "<br/> ";
}
Function _ destruct ()
{
Print "<br/> ";
Print "StrictCoordinateClass is being destroyed ";
}
Function _ get ($ property)
{
If (array_key_exists ($ property, $ this-> arr )){
Return $ this-> arr [$ property];
} Else {
Print "Error: Can't read a property other than x & y \ n ";
}
}
Function _ set ($ property, $ value)
{
If (array_key_exists ($ property, $ this-> arr )){
$ This-> arr [$ property] = $ value;
} Else {
Print "Error: Can't write a property other than x & y \ n ";
}
}
}
$ Obj = new StrictCoordinateClass ();
$ Obj-> x = 1;
Print $ obj-> x;
Print "<br/> ";
$ Obj-> n = 2;
Print "<br/> ";
Print $ obj-> n;
?>

Output result:
StrictCoordinateClass is being created
1
Error: Can't write a property other than x & y
Error: Can't read a property other than x & y
StrictCoordinateClass is being destroyed
_ Construct () and _ destruct () are equivalent to constructor in Java and destructor in C.
For the _ get and _ set parameters, see the following:
Reference: http://www.phpchina.com/html/54/26354-31906.html
Application of four methods:. _ set () _ get () _ isset () _ unset ()
In general, the class attribute is always defined as private, which is more in line with the actual logic. However, attributes are read and assigned frequently. Therefore, in PHP5, we predefine the two functions "_ get ()" and "_ set () to obtain and assign values to the attributes, and check the "_ isset ()" and the method "_ unset ()" for deleting the attributes ()".
In the previous section, we set and obtain each attribute. In PHP5, we provided a method specifically for setting and obtaining values for the attribute, "_ set () "and" _ get () "methods. These two methods do not exist by default, but are manually added to the class, like the constructor (_ construct (), the class will only exist after it is added. You can add these two methods in the following way. Of course, you can also add them in your own style:
// _ 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;
}
_ Get (): This method is used to obtain the attribute value of a private member. There is a parameter that is used to input the name of the member attribute you want to obtain and return the obtained attribute value, this method does not need to be manually called, because we can also make this method a private method, which is automatically called when the object directly obtains the private property. Because private attributes have been encapsulated, values cannot be obtained directly (for example, "echo $ p1-> name" is incorrect ), however, if you add this method to the class, when you use a statement such as "echo $ p1-> name" to directly obtain the value, it will automatically call _ get ($ property_name) to pass the attribute name to the parameter $ property_name. The value of the private attribute we passed in is returned through the internal execution of this method. If the member attributes are not encapsulated as private, the object itself will not automatically call this method.
_ Set () method: This method is used to set values for private member attributes. There are two parameters. The first parameter is the attribute name for the set value, the second parameter is the value to be set for the attribute, with no return value. This method does not need to be called manually. It can also be made private. It is automatically called when private property values are directly set. Similarly, the private property has been encapsulated.
The _ set () method is not allowed, for example, $ this-> name = 'hangsan, however, if you add the _ set ($ property_name, $ value) method to the class, it will be automatically called when the private property is assigned a value directly, pass the attribute such as name to $ property_name, and pass the value "zhangsan" to $ value. This method is used to assign values. If the member attributes are not encapsulated as private, the object itself will not automatically call this method. In order not to pass in invalid values, you can also make a judgment in this method. The Code is as follows:
<? Php
ClassPerson
{
// The following are the member attributes of a person, all of which are encapsulated private members.
Private $ name; // name of a person
Private $ sex; // gender of a person
Private $ age; // age of a person
// _ Get () is used to obtain private attributes.
Private function _ get ($ property_name)
{
Echo "the _ get () method is automatically called when the private property value is directly obtained. <br> ";
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)
{
Echo "when setting private property values directly, the _ set () method is automatically called to assign values to private properties. <br> ";
$ This-> $ property_name = $ value;
}
}
$ P1 = newPerson ();
// Operations that directly assign values to private properties automatically call the _ set () method to assign values.
$ P1-> name = "James ";
$ P1-> sex = "male ";
$ P1-> age = 20;
// Directly obtain the value of the private attribute. The _ get () method is automatically called to return the value of the member attribute.
Echo "name:". $ p1-> name. "<br> ";
Echo "Gender:". $ p1-> sex. "<br> ";
Echo "age:". $ p1-> age. "<br> ";
?>

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.