This article mainly introduces the class modifiers and access modifiers of PHP, which has certain reference value. Now I share it with you. Friends in need can refer to it
1. The class modified by abstract is an abstract class. If a class contains an abstract method, then the class is an abstract class (of course there is no abstract method in a class, we can also define this class as an abstract class ), so what is an abstract method? An abstract method is a method without a method body (without curly braces and content inside). Abstract methods are also modified in front of them. The abstract class itself cannot be instantiated, only a class inherits it and overrides all its abstract methods, so that we can instantiate its subclasses
abstract class My{ abstract function say(); } // abstract class My{ function say(){ echo 'hello'; } }
2. The class modified by interface is an interface. Interfaces are somewhat similar to abstract classes, but their difference is that all methods of the interface are abstract methods. There is no abstract modification in front of the abstract methods of the interface, and the member attributes must be constants. . Interface references are different from class inheritance keyword extends. Inheritance can only be single, while interfaces can use the keyword implements to have multiple references separated by commas.
interface demo { const NAME = "常量对象属性"; function fun1(); function fun2(); //抽象方法。 } class MyPc extends Root implements demo , demo2 , demo3 { ... }
3. The class modified by final is a terminal class and cannot be inherited.
1.public public methods and properties can be used in all classes
2.static static methods and variables
If access control permissions allow, you can use the class directly without creating an object of this class When called by adding two colons "::" to the name, there is only one copy of the static attribute in the memory, which is shared by all instances. Static methods cannot call non-static attributes. Non-static properties cannot be called using self::.
function test(){ static $a=1; $a*=2; echo $a."\n"; } test() test() test() //打印结果 //2 //4 //8
The initialization assignment of static variables is only once, and subsequent test methods will not initialize it again.
3. Private modified methods and variables are private and can only be called in this class (when this class is instantiated in other classes, private modified methods and variables cannot be called) Variables)
How to quickly generate modern forms with PHP
php implements shared memory process communication function (_shm )
The above is the detailed content of PHP class modifiers and access modifiers. For more information, please follow other related articles on the PHP Chinese website!