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 a reusable object-oriented class library.
Like C + + and Java, PHP has three ways to access it: public,private and protected. This can be one of the ways that a class member is accessed. If you do not specify the access method, the default access method is public. You can also specify a way to access static members, placing access before the static keyword, such as public static.
Public members can be accessed without restrictions. Any code outside the class can read and write to the public property. You can call a public method from anywhere in the script. In the first few versions of PHP, all methods and properties are public, which makes the object seem like an array of ingenious structures.
Private (private) members are visible only inside the class, and you cannot change or read its value outside of the class method where the private property resides. Similarly, only methods in the same class can invoke a private method, and inherited subclasses cannot access private members in the parent class.
Note that any member of the class and an instance of the class can access the private member. Look at the example 6.8,equals method to compare two widgets. The = = operator Compares two objects of the same class, but each object instance in this example 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, and 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 is the same check if 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 n");
}
FALSE
if ($w 1->equals ($w 3))
{
Print ("W1 and W3 are the same n");
}
FALSE, = = includes ID in comparison
if ($w 1 = = $w 2)//range because the ID is different
{
Print ("W1 and W2 are the same n");
}
? >
If you are not familiar with programming to objects, you may want to know what the purpose of using private members is. You can recall the idea of encapsulation and coupling, which we have 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, and they also help to achieve loose coupling. If code outside the data structure does not directly access internal properties, then there is no implicit association.
Of course, most private properties can still be shared by external code. The workaround is to use a pair of public methods, one is get (gets the value of the property) and the other is set (the value of the property is set). The constructor also accepts the initial value of the property. This allows for communication between members through a narrow, well-defined interface. This also provides an opportunity to change the value passed to the method, noting in example 6.8 how the constructor forces the price to be a float number (Floadval ()).
A Protected (protected) member can be accessed by all methods in the same class and all methods in the inherited class. The public attribute is in the spirit of encapsulation, because they allow subclasses to rely on a particular property to write, and the protected method does not cause this concern, and a subclass that uses the protected method needs to be clear about the structure of its parent class.
Example 6.9 is improved by example 6.8 and contains the subclass thing of a widget. Note that the widget now has a protected method called GetName. If an instance of the widget tries to invoke the protected method, an error is generated: $w 1->getname () produces a fault, but the GetName method in the subclass thing can invoke the protected method. Of course for the proof widget: GetName method is protected, this example is too simple, in practice, using the protected method depends on the understanding of 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 widgets is 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 ());
}
}
$w 1 = new Widget (' Cog ', 5.00);
$w 2 = new Thing (' Cog ', 5.00);
$w 2->setcolor (' Yellow ');
True (still!) The result is still true
if ($w 1->equals ($w 2))
{
Print ("W1 and W2 are the same n");
}
Print COG Output COG
Print ($w 2->getname ());
? >
A subclass may change the way the method is accessed by overwrite the parent class method, although there are still some limitations. If you overwrite a public class member, you must keep public in his 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 the private member of the parent class will simply create a different member 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 a method that is identified as final in a parent class, and the final keyword cannot be used for attributes.