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 the access method, access is by default The method is public. You can also specify an access method for static members by placing the access method before the static keyword (such as public static).
Public members can be accessed without restrictions. Any code outside the class can read and write public properties. You can call a public method from anywhere in your script. In previous versions of PHP, all methods and properties were public, which made objects feel like nicely structured arrays.
Private members Visible only within the class. You cannot change or read the value of a private property outside the class method in which it resides. Likewise, only methods within the same class can call a private method. Neither can inherited subclasses. Access private members in the parent class.
Note that any member in the class and instances of the class can access private members. See Example 6.8, the equals method compares two widgets. The == operator compares two widgets of the same class. Object, 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 property of another Widget instance. Both Java and C allow this operation.
<?php
classWidget
{
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 samen");
}
//FALSE
if($w1->equals($w3))
{
print("w1 and w3 are the samen");
}
//FALSE, == includes id in comparison
if($w1 == $w2) //not equal because the IDs are different
{
print("w1 and w2 are the samen");
}
?>
If you are new to object-oriented programming, you may be wondering what the purpose of using private members is. You may recall the ideas of encapsulation and coupling, which we discussed at the beginning of this chapter. Private members help encapsulate data. They can be hidden inside a class without being accessed by code outside the class. They also help achieve loose coupling. If code outside the data structure cannot directly access the internal properties, then there will not be an implicit relationship.
Ofcourse
, most private properties can still be shared by external code. The solution is to use a pair of public methods, one is get (get the value of the property), the other is set (set the value of the property). Constructors also accept properties initial value. This allows communication between members to occur through a narrow, well-qualified interface. This also provides the opportunity to change the value passed to the method. Note in Example 6.8 how the constructor forces price to be a float Number (floadval()).
Protected members can be accessed by all methods in the same class and in inherited classes. Public properties violate the spirit of encapsulation because they allow subclasses to depend on Writing a specific attribute to the protected method does not bring this concern. A subclass using the protected method needs to be clear about the structure of its parent class.
Note that Widget now has a protected method called getName. If an instance of Widget tries to call the protected method, an error will occur: $w1->getName() generates an error. But the getName method in the subclass Thing can call this protected method. Of course, to prove that the Widget::getName method is protected, this The example seems too simple. In actual situations, using protected methods depends on understanding the internal structure of the object.
<?php
classWidget
{
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
public function equals($widget)
{
return(($this->name == $widget->name)AND
($this->price == $widget->price));
}
protected function getName()
{
return($this->name);
}
}
class Thing extends Widget
{
private $color;
public function setColor($color)
{
$this->color = $color;
}
public function getColor()
{
return($this->color);
}
public function getName()
{
return(parent::getName());
}
}
$w1 = new Widget('Cog', 5.00);
$w2 = new Thing('Cog', 5.00);
$w2->setColor('Yellow');
//TRUE (still!) The result is still true
if($w1->equals($w2))
{
print("w1 and w2 are the samen");
}
//print Cog output Cog
print($w2->getName());
?>
A subclass may change the way the method is accessed by overriding the 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 After declaring 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 in the current class. Therefore, technically you cannot override a private member.
The Final keyword is another way to restrict access to member methods. A subclass cannot override a method that is marked final in a parent class. The Final keyword cannot be used for properties.