PHP5 access 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 access, you can develop a reliable object-oriented application and build reusable object-oriented class libraries.
Like C + + and Java, PHP has three ways to access it: public,private and protected. One of the ways in which a class member can be accessed. If you do not specify the way to access, the default access mode is public. You can also specify a way for static members to be accessed by placing the access method before the static keyword (for example, public static).
Public members can be accessed without restrictions. Any code outside of a class can read and write to the public property. You can invoke a public method from anywhere in the script. In the first few versions of PHP, all of the methods and properties are public, which makes people feel like an array of elaborate structures.
Private (private) members are visible only inside the class. You cannot change or read the value of a private property outside of the class method in which it is located. Similarly, only methods in the same class can invoke a private method. Inherited subclasses also cannot access private members in the parent class.
Be aware that any member of a class and an instance of a class can access private members. See example 6.8,equals method compares two widgets. The = = operator Compares two objects of the same class, but in this example each object instance has a unique Id.equals method that compares name and price only. Note how the Equals method accesses the private property of another widget instance. Both Java and C allow such an operation.
Listing 6.8 Private Members
<?php
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 that two widgets are the same
Public function equals ($widget)
{
return ($this->name = = $widget->name) and
($this->price = = $widget->price));
}
}
$w 1 = new Widget (' Cog ', 5.00);
$w 2 = new Widget (' Cog ', 5.00);
$w 3 = new Widget (' Gear ', 7.00);
TRUE
if ($w 1->equals ($w 2))
{
Print ("W1 and W2 are the Same<br>n");
}
FALSE
if ($w 1->equals ($w 3))
{
Print ("W1 and W3 are the Same<br>n");
}
FALSE, = = includes ID in comparison
if ($w 1 = = $w 2) file://unequal because the ID is different
{
Print ("W1 and W2 are the Same<br>n");
}
?> a subclass may change the way the method is accessed by overridden the parent class method, however, there are still some limitations. If you overwrite a public class member, he or she must remain public in the subclass. If you overwrite a protected member, it can remain protected or become public. Private members are still visible only in the current class. Declaring a member with the same name as a private member of the parent class will simply create a different member than the original in the current class. Therefore, technically you cannot overwrite a private member.
The final keyword is another way to restrict access to member methods. Subclasses cannot overwrite methods that are identified as final in the parent class. The final keyword cannot be used for attributes. Haohappy Note: PHP5 's object-oriented model is still not perfect, as final is not as good for data,method or even class as in Java.
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