/*
+-------------------------------------------------------------------------------+
| = This article is read by Haohappy<<Core PHP Programming>>
| =Notes from the chapter Classes and Objects
| = Translation as the main + personal experience
| = To avoid unnecessary troubles that may occur, please do not reprint, thank you
| = Criticism and correction are welcome, and I hope to make progress with all PHP enthusiasts!
| = PHP5 Research Center:/haohappy2004
+-------------------------------------------------------------------------------+
*/
Section 8--Access Method
The access method of PHP5 allows restricting access to class members. This is a new feature added in PHP5, but it has existed in many object-oriented languages. With access method, a reliable object-oriented application can be developed and a reusable object-oriented class library can be built.
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 access method is public by default. You can also specify an access method for static members, putting the access method before the static keyword (such as public static).
Public members can be accessed without limitation. Any code outside the class can read and write public properties. You can call a public method from anywhere in the script. In the first few versions of PHP, all methods and properties were public, which makes people feel that objects are like arrays with exquisite structures.
Private (private) members are only visible inside the class. You cannot change or read its value outside the class method where a private property resides. Similarly, only 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 instance of the class can access private members. See 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 method to compare name and price. Pay attention to how the equals method accesses the private attribute of another widget instance. Both Java and C allow such operations.
Listing 6.8 Private members
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) and the other is set (set the value of the property). The constructor also accepts the initial value of the property. This allows communication between members to be carried out through a narrow, well-defined interface. This also provides an opportunity to change the value passed to the method. Note how the constructor forces price to become a float number (floadval()).
Protected members can be accessed by all methods in the same class and all methods in the inherited class. Public properties violate the spirit of encapsulation because they allow subclasses to rely on a specific property to write. Protected methods do not cause this concern. A subclass using protected methods needs to be very clear about the structure of its parent class.
Example 6.9 is improved from Example 6.8, which contains a subclass of Widget. 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() produces an error. But the getName method in the subclass Thing can call this protected method. Of course, this example seems too simple to prove that the Widget::getName method is protected. In actual situations, using the protected method depends on the understanding of the internal structure of the object.
Listing 6.9 Protected members
A subclass may change the access method by overwriting the parent class method. Still, there are some limitations. If you overwrite a member of a public class, it must remain public in its subclass. If you overwrite a protected member, it can remain protected or become a member still only visible in the current class. Declaring a member with the same name as the parent class's private member will simply create a member in the current class that is different from the original one. Therefore, technically you cannot overwrite a private member.
The Final keyword is another way to restrict access to member methods. A subclass cannot overwrite a method identified as final in the parent class. The Final keyword cannot be used for attributes.
+-------------------------------------------------------------------------------+
| = This article is read by Haohappy<<Core PHP Programming>>
| =Notes from the chapter Classes and Objects
| = Translation as the main + personal experience
| = To avoid unnecessary troubles that may occur, please do not reprint, thank you
| = Criticism and correction are welcome, and I hope to make progress with all PHP enthusiasts!
| = PHP5 Research Center:/haohappy2004
+-------------------------------------------------------------------------------+
*/
Section 8--Access Method
The access method of PHP5 allows restricting access to class members. This is a new feature added in PHP5, but it has existed in many object-oriented languages. With access method, a reliable object-oriented application can be developed and a reusable object-oriented class library can be built.
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 access method is public by default. You can also specify an access method for static members, putting the access method before the static keyword (such as public static).
Public members can be accessed without limitation. Any code outside the class can read and write public properties. You can call a public method from anywhere in the script. In the first few versions of PHP, all methods and properties were public, which makes people feel that objects are like arrays with exquisite structures.
Private (private) members are only visible inside the class. You cannot change or read its value outside the class method where a private property resides. Similarly, only 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 instance of the class can access private members. See 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 method to compare name and price. Pay attention to how the equals method accesses the private attribute of another widget instance. Both Java and C allow such operations.
Listing 6.8 Private members
Copy the codeThe code is as follows:
<?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 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<br>\n");
}
//FALSE
if($w1->equals($w3))
{
print("w1 and w3 are the same<br>\n");
}
//FALSE, == includes id in comparison
if($w1 ==$w2)//Different, because the IDs are different
{
print("w1 and w2 are the same<br>\n");
}
?>
If you are not familiar with object-oriented programming, you may want to know what the purpose of using private members is. You can recall the idea 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 exposed to code outside the class. At the same time, they also help to achieve loose coupling. If code outside the data structure cannot directly access internal properties, then there will be no implicit correlation.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<br>\n");
}
//FALSE
if($w1->equals($w3))
{
print("w1 and w3 are the same<br>\n");
}
//FALSE, == includes id in comparison
if($w1 ==$w2)//Different, because the IDs are different
{
print("w1 and w2 are the same<br>\n");
}
?>
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) and the other is set (set the value of the property). The constructor also accepts the initial value of the property. This allows communication between members to be carried out through a narrow, well-defined interface. This also provides an opportunity to change the value passed to the method. Note how the constructor forces price to become a float number (floadval()).
Protected members can be accessed by all methods in the same class and all methods in the inherited class. Public properties violate the spirit of encapsulation because they allow subclasses to rely on a specific property to write. Protected methods do not cause this concern. A subclass using protected methods needs to be very clear about the structure of its parent class.
Example 6.9 is improved from Example 6.8, which contains a subclass of Widget. 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() produces an error. But the getName method in the subclass Thing can call this protected method. Of course, this example seems too simple to prove that the Widget::getName method is protected. In actual situations, using the protected method depends on the understanding of the internal structure of the object.
Listing 6.9 Protected members
Copy the codeThe code is as follows:
<?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
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<br>\n");
}
//print Cog Output Cog
print($w2->getName());
?>
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<br>\n");
}
//print Cog Output Cog
print($w2->getName());
?>
A subclass may change the access method by overwriting the parent class method. Still, there are some limitations. If you overwrite a member of a public class, it must remain public in its subclass. If you overwrite a protected member, it can remain protected or become a member still only visible in the current class. Declaring a member with the same name as the parent class's private member will simply create a member in the current class that is different from the original one. Therefore, technically you cannot overwrite a private member.
The Final keyword is another way to restrict access to member methods. A subclass cannot overwrite a method identified as final in the parent class. The Final keyword cannot be used for attributes.