Section 8 access method [8] _ PHP Tutorial

Source: Internet
Author: User
Section 8 access method [8]. PHP5 allows you to restrict access to class members. this is a new feature in PHP5, but it already exists in many object-oriented languages. with the access method, you can develop a reliable PHP5 access method to allow access to class members. this is a new feature in PHP5, 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 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 be accessed without any restriction. any code outside the class can read and write the public attribute. you can call a public method from anywhere in the script. in the first few versions of PHP, all methods and attributes are public, which makes people think 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 its class method. similarly, only a method in the same class can call a private method. the inherited subclass cannot access the private member of 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. note how the equals method accesses the private attribute of another Widget instance. both Java and C allow such operations.

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) file: //, because the ID is different
{
Print ("w1 and w2 are the same
N ");
}
?> A subclass may change the method of access by overwriting the parent class method. However, there are still some restrictions. if you overwrite a public class member, its subclass must be public. if you overwrite a protected member, it can remain protected or public. the Private member is only visible in the current class. declaring a member with the same name as the private member of the parent class will create a different member in the current class. therefore, technically, you cannot override a private member.

The Final keyword is another method to restrict access to members. the subclass cannot override the method identified as final in the parent class. the Final keyword cannot be used for attributes. // haohappy note: The PHP5 object-oriented model is still incomplete. for example, final can be used for Data, Method, and even Class as in Java.

Features. this is a new feature in PHP5, but it already exists in many object-oriented languages. with access methods, you can develop a reliable one...

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.