The article is primarily intended for novice developers, or for those who are just beginning to move from the procedural programming style to the PLO, therefore, please do not drive the gurus into minus :)
The rights of access to properties and methods are at first glance just three words: private, protected, and public. But what is behind them? What advantages does it have in development? And how to use them correctly? Here, as in all other aspects of programming, one cannot understand without practice ...
One of the three main concepts of OOP is inheritance (the other two are encapsulation and polymorphism). Vobshchem the access rights were realized for her. The underlying idea of inheritance: A child object, when inheriting (extend) a parent, adopts all the parent methods and properties, and can also acquire its own. Understanding this base, you can go into everything that is below ...
')
Private - declares a method or property available only in the class in which it is present. To private methods and properties, we can not access either from objects or from child classes.
Protected - declares a method or property protected. Ie such that can not be accessed from the object that implements the class, but it can be used in child classes.
Public - public. Classes and methods declared public can be accessible both within the class itself, and in child classes and in objects that implement the class.
Just want to note that with inheritance, access methods can only change to more loyal. ie, in the following sequence, but not back: private → protected → public
Also, methods can be
final , those which cannot be redefined in descendant classes.
In general, all access methods are used exclusively for self-documentation of the code and do not carry any logical component, so even without them, life is only hard, but not impossible, as PHP4 proves, in which all methods and properties were public ...
Practice
Sometimes there are situations when these access methods are not enough. Ie, for example, we may want to have access from an object to read some property, but not be able to write to it. The simplest solution is to declare a public property and add a comment
/ * read-only * / , but you can unintentionally forget about a comment and spoil the logic of the program's behavior, wedged with a non-standard value in the middle of execution. then it is time to use getters (getter \ 's). A getter is nothing more than a class method that implements an exceptional ability to read non-public properties from an object. Here is an example:
class A {
private $a = 7;// ,
public function getA() { //
return $this->a; //
}
}
$obj = new A();
echo $obj->getA();// $a
The setters (setter \ 's) also behave in a similar way when we need to be able to set the value of a variable, but not read it directly, since, for example, it must be converted before being used. Example setter method:
//...
public funtion setA($value) { //
$this->a = $value; // $a ,
}
//...
Another way to implement access to methods, when the method should be read-only
from everywhere , is to introduce the \ "pseudo-properties \":
class A {
public function getValue() {
static $value;
if (empty($value)) {
$value = //... -
}
return $value;
}
}
in the example above, class A will have the $ value pseudo-property. Pseudo - because it is implemented exclusively through the method, and access to it is possible only for reading. You may also notice that I used the pattern \ "lazy initialization \" to postpone the creation of the property until the last moment and at the same time, as if \ "cache \" it. Where it can be applied is well illustrated in the adjacent topic about OOP in PHP.
It’s good practice to hide all properties using the private method and, depending on the needs, create setters or getters for them, but you need to be careful that if there is both a setter and a getter for the property and there is no additional processing logic, is it not easier to remove them , and property to make public? :)