PHP5's access method allows restricting access to class members. This is a new feature in PHP5, but has long existed in many object-oriented languages. With access methods, you can develop a reliable object-oriented application and build a reusable object-oriented class library.
Like C++ and Java, PHP has three access methods: public, private and protected. The access method for 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 provide Static members specify an access method. Place 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 read and write public properties from the script. Call a public method anywhere. In previous versions of PHP, all methods and properties were public, which made objects look like nicely structured arrays.
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 resides. Similarly, only methods in the same class can call a private method, and inherited subclasses cannot access private members in the parent class.
Note that private members can be accessed by any member of the class and by instances of the class. 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. Notice how the equals method accesses the private property of another Widget instance. Both Java and C allow this.
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 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) //not equal because the IDs are different
{
print("w1 and w2 are the same n");
}
?>
If you are new to object-oriented programming, you may wonder 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 within a class from 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 is no implicit correlation.
Of course, 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 initial values for properties. This allows communication between members to occur through a narrow, well-qualified interface. This also provides the opportunity to change the values passed to the method. Note in Example 6.8 how the constructor coerces Make price a float number (floadval()).
Protected (protected) members can be accessed by all methods in the same class and all methods in inherited classes. Public properties go against the spirit of encapsulation because they allow subclasses to rely on a specific property to write. Protected methods do not bring this concern. A subclass using a protected method needs to be very aware of the structure of its parent class. Just fine.
Example 6.9 is improved from Example 6.8 and includes a Thing subclass of Widget. Note that Widget now has a protected method called getName. If an instance of Widget attempts 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, this example is too simple to prove that the Widget::getName method is protected. In actual situations, using the protected method depends on understanding the internal structure of the object.
Listing 6.9 Protected 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
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 same n");
}
//print Cog output Cog
print($w2->getName());
?>
A subclass may change the method access method by overriding the parent class method. However, there are still some restrictions. If you override a public class member, it must remain public in its subclasses. If you override 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. Subclasses cannot override methods marked as final in the parent class, and the Final keyword cannot be used for attributes.