& Lt ;? Php *************************************** * *** Class object instance description (common Class) **************************************** * ** classClassDe
- /*************************************** ****
- Class object instance description (common Class)
- **************************************** ***/
- Class ClassDemo {
- Public $ PublicVar;
- Private $ PrivateVar; // private variables cannot be called outside
- Protected $ ProtectedVar; // The externally protected variable is not accessible to the subclass.
- Public static $ StaticVar = 0; // static variables static methods cannot access non-static methods and variables, non-static methods and variables can access static methods and variables
- Const constVar = ''; // when you have an attribute that you do not want to modify, consider using const as a constant, using class name: constant name; interface name: constant
- /*
- 1. an initial value must be assigned to a constant during definition.
- 2. constants cannot be modified.
- 3. the constant name cannot have $, which is generally in upper case. the default value is public.
- 4. constants use the self: constant name inside the class. class name: constant name
- */
- Public function _ construct (/* $ name */) {// this can contain a parameter. it must also contain a parameter when a parameter is created. Otherwise, an error may occur.
- // $ This-> PublicVar = $ name;
- // Self: $ StaticVar ++; // static variable internal access method external access method object name: variable name (ClassDemo: $ StaticVar)
- $ This-> PublicVar = '$ PublicVar ';
- $ This-> PrivateVar = '$ PrivateVar ';
- $ This-> ProtectedVar = '$ ProtectedVar ';
- Self: $ StaticVar ++;
- Echo "ClassDemo _ construct ";
- }
- Public function _ destruct () {// destructor
- }
- Final public function fun (){
- /*
- Final keyword (in PHP5)
- If you do not want the method to be overwritten by the inheritance time of other classes, you can use final
- When this keyword is used to modify a class, the class cannot be inherited by other classes (can be instantiated)
- Note: This keyword cannot be used to modify variables.
- */
- }
- } // End Class
- /*************************************** ****
- Class object instance description (abstract Class)
- **************************************** ***/
- Abstract class AbstractDemo {
- /***************************************
- 1. abstract classes cannot be instantiated.
- 2. abstract classes do not have to include abstract methods.
- That is to say, the abstract class can have no abstract method.
- 3. Once the abstract method is included, this class must
- Declare as abstract
- 4. abstract classes cannot have function bodies
- 5. if a class continues an abstract class, it must implement
- All abstract methods of this abstract class. (Unless it is declared as an abstract class)
- ***************************************/
- }
- /*************************************** ****
- Class object instance description (inherited Class)
- **************************************** ***/
- Class DemoTwo extends ClassDemo {
- }
- /*************************************** ****
- Class object instance description (interface)
- **************************************** ***/
- Interface Face {
- /*********************************
- 1. when a class implements an interface, it is required that the class must implement all methods of this interface.
- 2. methods of the interface cannot have a method body.
- 3. an interface cannot be instantiated.
- 4. the interface can have attributes, but must be a constant and public
- When to use the interface
- 1. set specifications
- 2. set standards for other programmers to implement, such:
- **********************************/
- Public function Name ();
- }
- Interface Face2 extends Face {
- /*******************************
- Inheritance interface
- The method for implementing the parent interface when inheriting interfaces
- ********************************/
- Const namevar = 20;
- }
- Class Demo implements Face2 {
- /**********************************
- Implementation interface. multiple interfaces can be implemented at the same time.
- When a class implements some interfaces, all interfaces and methods must be implemented.
- **********************************/
- Public $ Name1 = 0;
- Public function Name (){
- Echo Face2: namevar;
- }
- }
- ?>
Summary:Use self: (note not $ slef) to access static member attributes or methods within the class, for example:
Slef: $ country
Slef: myCountry ()
When the subclass accesses the static member attributes or methods of the parent class, use parent: (note not $ parent). The instance code is as follows:
Parent: $ country
Parent: myCountry ()
The attributes and methods of static members for external access are class name/subclass name: the instance code is as follows:
Person: $ country
Person: myCountry ()
Student: $ country
However, static methods can also be accessed through common objects.
• Class/object functions
• _ Autoload-try to load undefined classes
• Call_user_method_array-call a user method and pass the parameter array (obsolete)
• Call_user_method-call the user method for a specific object (obsolete)
• Class_alias-create an alias for a class
• Class_exists-check whether the class is defined
• Get_called_class-name of the Static Binding ("Late Static Binding") class
• Get_class_methods-returns an array composed of class method names
• Get_class_vars-returns an array composed of the default attributes of the class.
• Get_class-class name of the returned object
• Get_declared_classes-returns an array composed of the names of defined classes.
• Get_declared_interfaces-returns an array containing all declared interfaces.
• Get_declared_traits-returns an array of all defined traits
• Get_object_vars-returns an associated array composed of object attributes.
• Get_parent_class-return the parent class name of the object or class
• Interface_exists-check whether the interface has been defined
• Is_a-if the object belongs to this class or the class is the parent class of this object, TRUE is returned.
• Is_subclass_of-if this object is a subclass of this class, TRUE is returned.
• Method_exists-check whether the class method exists
• Property_exists-check whether the object or class has this attribute
• Trait_exists-check whether the specified trait exists