This article describes the variables and members of the PHP class in detail, and issues related to inheritance, access, and rewriting. If you need them, refer to them. This article describes the variables and members of the PHP class in detail, and issues related to inheritance, access, and rewriting. If you need them, refer to them.
Script ec (2); script
PHP5.3-based
PHP classes and their instances:
The Code is as follows: |
|
Class Myclass {
Public $ prop = 123; } $ Obj = new Myclass (); |
The member attributes of a class (the attribute name is relative to the "method") include class constants and class variables. Class constants cannot be empty during definition, if a class attribute is assigned a value during definition, it can only use scalar and array, and cannot be an expression. Because the class attribute is initialized during compilation, PHP does not execute expressions during compilation.
1. Access Control for members:
Public: It can be inherited and can be accessed outside the class's methods, such as $ obj-> prop;
Protected: It can be inherited and cannot be accessed outside of class methods.
Private: it cannot be inherited or accessed outside the class method.
PHP 4 uses var to declare the attributes of the class, which is no longer used after PHP5. It is warned before PHP5.3, and PHP5.3 can be used before public or separately as the public alias.
These three access control keywords can also be used to modify constructors. When private and protected modifier class constructors, you can only call constructors through a publice static method to instantiate objects, this is because the function cannot be accessed outside the class. For example, the implementation of a singleton class:
The Code is as follows: |
|
Class Singleton { Private static $ instance = null; Public $ k = 88; Private function _ construct (){ } Public static function getInstance (){ If (self: $ instance = null ){ Self: $ instance = new self (); } Return self: $ instance; } Public function _ clone () {// pretend clone oprationg Throw ('singleton class can not be cloned '); Return self: getInstance (); } } // New Singleton (); // Error $ In = Singleton: getInstance (); |
2. Inheritance prohibited: The final keyword, used only for modifying methods of classes or classes
If a class is modified by final, this class cannot be inherited. If a method is modified by final, this method cannot be overwritten ).
The Code is as follows: |
|
Class Myclass { Public $ prop = 123; Final public static function methodA () {// public static method that cannot be inherited Return 'this is a final method '; } } |
3. abstract classes and abstract METHODS: abstract is only used for classes and Methods. abstract classes cannot be used directly to instantiate objects and can only be used to generate subclasses.
The Code is as follows: |
|
Abstract class Myclass { Public $ prop = 123; Abstract public function methodA (); // The abstract method does not implement the function body. } |
Homepage 1 2 3 last