In-depth Exploration of object models in PHP5.0

Source: Internet
Author: User
PHP5's visit method promised to restrict visits to class members. This is a new function in PHP5, but it already exists in many object-oriented languages. With a visit method, we can develop a reliable object-oriented application and build reusable aspects.

PHP5's visit method promised to restrict visits to class members. This is a new function in PHP5, but it already exists in many object-oriented languages. With a visit method, we can develop a reliable object-oriented application and build reusable object-oriented class libraries.

Like C and Java, PHP has three access methods: public, private, and protected. the method of visiting a class member can be one of them. if you do not specify a visit method, the default visit method is public. you can also think that static members refer to a method of visiting, which should be placed before the static keyword (such as public static ).

Public Members can access any code outside the class without any restriction and can read or write the public attribute. you can call a public method from any part of the script. In the first few versions of PHP, all methods and attributes are public, which makes people feel that the object is like an exquisite array.

Private (private) members are only visible within the class. you cannot change or read the value of a Private property outside of the class method. Similarly, only the method in the same class can call a private method, and the continuous subclass cannot visit the private member in the parent class.

Pay attention to the fact that any member of the class and the instance of the class can visit the private member. In example 6.8, the equals method compares two widgets. = the operator compares two objects in the same class, but in this example, each object instance has a unique ID. the equals method only matches the name and price. Pay attention to how the equals method accesses the private attribute of another Widget instance. both Java and C agree to this control.

Listing 6.8 Private members

Class Widget
{
Private $ name;
Private $ price;
Private $ id;

Public function _ construct ($ name, $ price)
{
$ This-> name = $ name;
$ This-> price = floatval ($ price );
$ This-> id = uniqid ();
}
// Checks if two widgets are the same check whether the two widgets are the same
Public function equals ($ widget)
{
Return ($ this-> name = $ widget-> name) AND ($ this-> price = $ widget-> price ));
}
}
$ W1 = new Widget ('Cog', 5.00 );
$ W2 = new Widget ('Cog', 5.00 );
$ W3 = new Widget ('Gear ', 7.00 );

// TRUE
If ($ w1-> equals ($ w2 ))
{
Print ('w1 and w2 are the same n ');
}

// FALSE
If ($ w1-> equals ($ w3 ))
{
Print ('w1 and w3 are the same n ');
}

// FALSE, = Your des id in comparison
If ($ w1 = $ w2) // different, because the ID is different
{
Print ('w1 and w2 are the same n ');
}
?>


If you are not familiar with object-oriented programming, you may want to know what the goal of private members is. you can think back to the idea of encapsulation and coupling, which we have discussed at the beginning of this chapter. Private members help encapsulate data. they can hide inside a class without being exposed to code outside the class. they also help to implement loose coupling. if the code outside the data structure cannot directly visit internal properties, there will be no implicit correlation.

Of course, most private attributes can still be shared by external code. the solution is to use a pair of public methods, one is get (get attribute value), and the other is set (set attribute value ). The structure function also receives the initial attribute values. this makes the exchange between members through a narrow, well-defined interface. this also provides the opportunity to pass the value of the transformation to the method. in example 6.8, how the structure function forces the price to be a float number (floadval ()).

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.