Some Tips for access control in PHP classes
Most object-oriented programming languages, including PHP, provide access control over variables or methods of a class. This is the basis for implementing object-oriented encapsulation capabilities. Variables are data, and method functions are operations on that data, and according to the least knowledge rule, some data you don’t need to know exists. In this case, you need to use private variables and methods that are accessible only to the class itself. And then there are variables and methods that have their own subclasses that need to be used, but that can’t be exposed, so we’re going to use protected, which means protected. Finally, it is time to expose public variables or methods that can be used by any subclass, whether inside or outside the class.
Let’s review the use of these three access control characters in terms of variable access control.
class A {
private $private;
protected $protected;
public $public;
public function setPrivate($p) {$this->private = $p;
}
public function setProtected($p) {$this->protected = $p;
}
public function setPublic($p) {$this->public = $p;
}
public function testA() {echo $this->private, '= = ='.$this->protected, '= = ='.$this->public, PHP_EOL;
}
}
class B extends A{
public function testB() {echo $this->private, '= = =';
echo $this->protected, '= = ='.$this->public, PHP_EOL; }}$a = new A();
// $a->private = 'a-private'; // atal error: Uncaught Error: Cannot access private property A::$private
$a->setPrivate('a-private');
// $a->protected = 'a-protected'; // atal error: Uncaught Error: Cannot access protected property A::$protected
$a->setProtected('a-protected');
$a->public = 'c-public';
$a->testA();
echo "Out side public:" . $a->public, PHP_EOL;
// echo "Out side private:" . $a->private, PHP_EOL; // Fatal error: Uncaught Error: Cannot access private property A::$private
// echo "Out side protected:" . $a->protected, PHP_EOL; // Fatal error: Uncaught Error: Cannot access protected property A::$protected
$b = new B();
$b->setProtected('b-protected');
$b->public = 'b-public';
$b->testB(); / / not b - private$b->setPrivate('b-private');
$b->testB(); / / not b - privateCopy the code
It is clear from the above code that variables other than public cannot be called or assigned directly outside the class. So we use the public method of setXXX() to assign values for $private and $protected. This is where the concept of encapsulation comes in. For example, in setPrivate() we can make a logical judgment on the $p variable passed to us to determine whether to assign the value to $private.
Class B inherits from class A, so it has access to the $public and $protected variables of class A, but note that the $private variable is not accessible. So even if we call setPrivate() and assign a value to $private, we still can’t get $private because B can’t access it. Some friends want to ask, this situation does not report wrong? Class B will look for the $private variable in its own scope, and if not, it will generate a local variable and assign it null.
So what should subclasses do to use $private?
class C extends A {
private $private;
public function testC() {echo $this->private, '= = ='.$this->protected, '= = ='.$this->public, PHP_EOL;
}
// public function setPrivate($p{/ /$this->private = $p; / /}}$c = new C();
$c->setProtected('c-protected');
$c->public = 'c-public';
$c->setPrivate('c-private');
$c->testC();
Copy the code
Don’t open the comment for the C class setPrivate() method, you’ll see that $private is still null. In other words, the $private variable with the same name is not an override of the parent class variable, but a new one is created in the scope of the class. The parent class’s setPrivate() method cannot, of course, access the subclass’s private variable, so the subclass overrides a setPrivate() method to assign its own $private variable.
Remember that a privately-decorated variable or method is only open to the current class
The same applies to modifications to methods.
class D {
public function testD() {$this->show();
}
private function show() {echo 'This is D', PHP_EOL;
}
}
class E extends D {
private function show() {echo 'This is E', PHP_EOL; }}$e = new E();
$e->testD(); // This is D
Copy the code
Subclass E calls the testD() method of its parent class D. testD() calls the privately-decorated show() method of its parent class D. It still calls the show() method of its own class D.
conclusion
The access control content is relatively simple, the most important is the private modifier need to pay attention to, the rest is actually relatively easy to understand. However, the more simple things are, the more basic, object-oriented can not be separated from the three simple access modifiers, they play a large part in modern software development, only to grasp them is the right way to learn.
Test code: github.com/zhangyue050…
Reference: www.php.net/manual/zh/l… www.php.net/manual/zh/l… www.php.net/manual/zh/l…