For details about the usage of keywords such as public, private, protected, and abstract in PHP, protectedabstract
Common keywords in PHP
PHP contains many keywords that restrict functions and classes. Commonly Used keywords include abstract, final, interface, public, protected, private, static, and so on, next we will analyze and sort out the usage of these items.
Keywords of variables and Methods: public, private, protected
Public has the largest permission, which can be used by sub-classes or called after instantiation,
Protected indicates that the access permission is only accessible in the subclass and the class.
Private indicates private, which can only be accessed in the current class.
<? Php // *** Define MyClass */class MyClass {public $ public = 'public'; protected $ protected = 'protected'; private $ private = 'private '; public function printHello () {echo $ this-> public; echo $ this-> protected; echo $ this-> private;} protected function pro_test () {var_dump (1) ;}}$ obj = new MyClass (); echo $ obj-> public; // This line can be executed normally // echo $ obj-> protected; // this line will generate a fatal error // echo $ obj-> private; // this line will also generate a fatal Error $ obj-> printHello (); // output Public, Protected, and Private $ obj-> pro_test (); // directly reports an error?>
Static Keywords of variables and Methods
Static is called when values or methods do not need to be instantiated in the class. static modified variables have the function of storing values, for example, the result of running without static is as follows:
<?phpfunction test(){ $var=1; echo $var."</br>"; $var++;}test();// 1test();// 1test();// 1?>
If we add static to the variable, it will become like this.
<?phpfunction test(){ static $var=1; echo $var."</br>"; $var++;}test();// 1test();// 2test();// 3?>
We may not be able to understand the benefits of doing so in PHP, so let's assume that the reader is familiar with JS and there is no static keyword in JS, so if we want to implement a program that can save each program operation result as the basis for the next operation, we need to write it like this.
var glo=0; function test(){ glo++; document.writeln(glo); } test(); test(); test();
In this way, glo will be leaked to global variables. If many variables are generated, memory leakage will occur (Memory leakage refers to the absence of extra memory space occupied by variables)
<script> var glo=0; function test(){ glo++; document.writeln(glo); } test();// 1 test();// 2 test();// 3 </script>
Therefore, compared to languages without static definitions, it has the advantage of maintaining variables, avoiding Memory leakage, and easily causing misuse of global variables (because the scope is not global)
$age=0;$age++;function test1() { static $age = 100; $age++; echo $age."</br>";}function test2() { static $age = 1000; $age++; echo $age."</br>";}test1(); // 101test2(); // 1001
Keyword final of class and Method
Final can only be used to modify the class and function. After using final, it cannot be inherited. For example, the following code will directly report an error.
class BaseClass { public $public = 'Public'; function test() { echo "BaseClass::test() called\n"; } final public function moreTesting() { echo "BaseClass::moreTesting() called\n"; }}class ChildClass extends BaseClass { public function moreTesting() { echo "ChildClass::moreTesting() called\n"; }}
Special keyword interface, abstract
The significance of interface lies in the standard programming style. If an interface is implemented, we must implement the method when using this interface class, it plays a role in unified naming.
Class can inherit multiple interfaces. The single inheritance between interfaces is implemented through extends. The relationship between class and interface is established through implements.
Sample Code:
<?phpinterface testA{ function funcA();}interface testB{ function funcB();}interface testC extends testA { function funcC();}class run implements testC ,testB { public function funcA() { // TODO: Implement funcA() method. } public function funcB() { // TODO: Implement funcB() method. } public function funcC() { // TODO: Implement funcC() method. }}?>
Abstract Functions are the same as interface, but all methods in the interface must be implemented. However, in the abstract-modified class, there can be one or more abstract modification methods, so we can understand that interface is a special case of abstract (when all methods are abstract methods, must be implemented ). Abstract has the following features:
1. As long as at least one method in the class uses the abstract keyword, this class is abstract and corresponding keywords need to be added.
2. abstract methods. There is only the declaration part of the method, and there is no method body.
However, in my opinion, abstract has the following scenarios in actual application:
1. Standardize naming rules for the public part during multi-person programming (the principle is the same as interface)
2. The implementation does not allow the parent class to be directly instantiated and used
The style code is as follows:
<? Phpabstract class shopping {public function buy () {echo "buy";} public function loan () {echo "loan ";}} class leslieBuy extends shopping {}// $ test1 = new shopping; // syntax error $ leslieSie = new leslieBuy; $ leslieSie-> loan (); // print the loan?>
The usage of public, private, protected, abstract and other keywords in PHP is all the content shared by Alibaba Cloud. I hope you can give us a reference and support for the customer's house.