Method and property access modifiers in PHP5

They are Public, Protected and Private, which can be known because they are already used in other object-oriented languages.

In this chapter we will see the new access modifiers to the methods and attributes of the objects that have been incorporated in PHP 5. These access modifiers are none other than the well-known public, protected and private modifiers, which are already available in other languages ​​such as Java.

One of the principles of object-oriented programming is encapsulation, which is a process by which the internal characteristics of an object are hidden from those elements that do not have to know about it. Access modifiers are used to indicate the permissions that other objects will have to access their methods and properties.

public modifier

It is the most permissive access level. It is used to indicate that the method or attribute of the class is public. In this case, this attribute can be accessed, to be viewed or edited, by any other element of our program. It is the modifier that is applied if nothing else is indicated.

Let’s see an example of a class where we have declared its elements, a method and a property, as public. This is the “dice” class, which has an attribute with its score and a method to roll the die for a new random score.

class given{ public $points; function __construct(){ srand((double)microtime()*1000000); } public function tirate(){ $this->dots=$randval = rand(1,6); } } $my_dice = new dice(); for ($i=0;$i<30;$i++){ $my_dice->throw(); echo “
They have exited ” . $my_dice->points . ” points”; }

We see the declaration of the given class and then a few lines of code to illustrate its operation. In the example, a loop is made 30 times, in which the die is rolled and the score that has been obtained is displayed.

See also  Photoshop Pen Tool

Since the $points attribute and the tirate() method are public, they can be accessed from outside the object, or what is the same, from outside the code of the class.

private modifier

It is the most restrictive access level. It is used to indicate that this variable can only be accessed from the object itself, never from outside. If we try to access a method or attribute declared private from outside the object itself, we will get an error message indicating that it is not possible to access that element.

If in the example above we had declared the method and property of the given class private, we would have received an error message.

Here we have another possible implementation of the given class, declaring the points attribute and the tirate() method as private.

class given{ private $points; function __construct(){ srand((double)microtime()*1000000); } private function tirate(){ $this->dots=$randval = rand(1,6); } public function give_me_new_score(){ $this->throw(); return $this->points; } } $my_dice = new dice(); for ($i=0;$i<30;$i++){ echo "
They have exited ” . $my_dice->give_me_new_score() . ” points”; }

We have had to create a new public method to operate with the die, because if it is all private there is no way to use it. The aforementioned method is give_me_new_score(), which performs the action of rolling the die and returns the value that has come up.

protected modifier

This indicates a medium access level and a little more special than the previous ones. It is used for the method or attribute to be public within the code of the class itself and of any class that inherits from the one where the method or property is protected. It is private and not accessible from anywhere else. That is, a protected element is public within the class itself and its inherited classes.

See also  Functions for searches with dates in Access

Later we will explain inheritance and can provide examples with the protected modifier.

conclusion

Many times the developer himself is the one who sets his criteria when applying the different access modifiers to attributes and methods. Little protection means that objects lose their encapsulation and with it one of the advantages of OOP. Higher protection can make it more laborious to generate the program code, but in general it is advisable.

Loading Facebook Comments ...
Loading Disqus Comments ...