In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces what the Tips of access control in the PHP class has, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.
Some Tips for access control in the PHP class
Most object-oriented programming languages, including PHP, provide access control to variables or methods of a class. This is the basis for implementing object-oriented encapsulation capabilities. Variables are actually data, and method functions are operations that deal with them. According to the principle of least knowledge, you don't need to know about some data. At this point, you need to use private private variables and methods, which can only be accessed by this class. Some variables and methods need to be used by their own subclasses, but cannot be exposed to the outside world, so we will use protected, which is protected. Finally, you expose public public variables or methods that can be used by internal, external, or inherited subclasses of the class.
Let's first review the role of these three access control characters through the access control of variables.
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 color privatekeeper; / / atal error: Uncaught Error: Cannot access private property A::$private
$a-> setPrivate ('a house private')
/ / $a-> protected ='a copyright protectedwoman; / / atal error: Uncaught Error: Cannot access protected property A::$protected
$a-> setProtected ('amurprotected`)
$a-> public = 'cripple'
$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 ('bMurprotected`)
$b-> public = 'bripple'
$b-> testB (); / / No b-private
$b-> setPrivate ('bmurprivate`)
$b-> testB (); / / No b-private
It is clear from the above code that no variables other than public can be called or assigned directly outside the class. So we use the public method of setXXX () to assign values to $private and $protected. Here comes the concept of encapsulation. For example, in setPrivate (), we can logically judge the passed $p variable and decide whether to assign a value to $private.
Class B inherits class A, so it can access the $public and $protected variables of class A, but note that the $private variable is not accessible. So even if you call the setPrivate () method to assign a value to $private, you still can't get the value of $private because B can't access it. There is a little friend to ask, this kind of situation is not wrong? Of course, there is no error. Class B will look for the $private variable in its own scope, and if it is not defined, it will generate a local variable and assign a null value.
So what should I do if the subclass uses $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 ('cmurprotected`)
$c-> public ='c house public.'
$c-> setPrivate ('c Mushprivate')
C-> testC ()
Do not open the comments for the class C setPrivate () method yet, you will find that $private is still null. That is, the $private private variable defined with the same name does not override the variable of the parent class, but creates a new one within the scope of this class. The setPrivate () method of the parent class certainly cannot access the private variable of the subclass, so the subclass also overrides a setPrivate () method to assign a value to its $private variable.
Remember one thing: private-modified variables or methods are only open to the current class
The modification of the method has the same effect.
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
The subclass E calls the testD () method of the parent class D, and the testD () method calls the private-decorated show () method. According to the above principle, it still calls its own class D show () method.
The content of access control is relatively simple, the most important thing is that the modifier private needs to pay attention to, the rest is actually relatively easy to understand. However, the more simple things are, the more basic they are, and object-oriented can not be separated from these three simple access modifiers. They carry a lot of weight in modern software development, and only by firmly mastering them is the correct way for us to learn.
This is the end of the Tips about access control in the PHP class. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.