In-depth exploration of Object Model in PHP 5.0

Source: Internet
Author: User

Php (as the mainstream development language) 5 allows access to class members to be restricted. This is a new feature in php (as the mainstream development language) 5, but it already exists in many object-oriented languages. With access, you can develop a reliable Object-oriented Application and build reusable object-oriented class libraries.

Like C ++ and Java, php (as the mainstream development language) has three access methods: public, private, and protected. the access method of a class member can be one of them. if you do not specify the access method, the default access method is public. you can also specify an access method for a static member before the static keyword (such as public static ).

Public Members can access any code outside the. class without any restriction to read or write the public attribute. You can call a public method anywhere in the script. In the first few versions of php (as the mainstream development language), all methods and attributes are public, which makes people feel that objects are like arrays with exquisite structures.

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

Note that any member or instance of the class can access the private member. See 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 compares name and price. Pay attention to how the equals method accesses the private attribute of another Widget instance. Both Java and C allow this operation.

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 (gears, 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 purpose of using private members is. You can recall the idea of encapsulation and coupling, which we have discussed at the beginning of this chapter. Private members help encapsulate data. They can be hidden inside a class without being exposed to code outside the class. They also help to implement loose coupling. if code outside the data structure cannot directly access internal properties, there will be no implicit associations.

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.