PHP5's access methods allow you to restrict access to class members. This is a new feature in PHP5, but it has already existed in many object-oriented languages. With access methods, you can develop a reliable object-oriented application and build Reusable object-oriented class library.
Like C++ and Java, PHP has three access methods: public, private and protected. For the access method of a class member, it can be one of them. If you do not specify Access method, the default access method is public. You can also specify an access method for static members and put the access method before the static keyword (such as public static).
Public members can be accessed without restrictions Public properties can be read and written by any code outside the class. You can call a public method from anywhere in the script. In previous versions of PHP, all methods and properties were public, which made it feel like the object was Like a well-structured array.
Private (private) members are only visible inside the class. You cannot change or read the value of a private property outside the class method where it is located. Similarly, only within the Methods in the same class can call a private method. Inherited subclasses cannot access private members in the parent class.
It should be noted that any member in the class and instances of the class can access private members. Looking at Example 6.8, the 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. The equals method only compares name and price. Note that equals How to access the private properties 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 Checks if 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, == includes id in comparison
if($w1 == $w2) file:// is not equal because the IDs are different
{
print("w1 and w2 are the same
n");
}
?>
A subclass may change the way a method is accessed by overriding a parent class method. However, there are still some restrictions. If you override a public class member, it must remain public in the subclass. If you override a A protected member 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 in the current class. Therefore, Technically you cannot override a private member. The
Final keyword is another way to restrict access to member methods. Subclasses cannot override methods marked final in the parent class. The Final keyword cannot be used properties.
//haohappy Note: The object-oriented model of PHP5 is still not perfect enough. For example, final cannot be used for Data, Method or even Class like in Java.