Concept of class: A class is a collection of objects with the same attributes and operations. It provides a unified abstract description for all objects belonging to this class, which includes two main parts: attributes and operations. In object-orientedprogramming language, a class is an independent program unit. It should have a class name and include two main parts: attribute description and operation description.
1. Class definition:
i. The keyword definition of a class uses class
1. Define an empty class
1 |
|
2. Define an Classes with member attributes and operations
1 2 3 4 |
|
3. To define a class that cannot be inherited, use the final keyword
1 2 3 4 |
|
4. Note: the final keyword cannot be used to modify member attributes, only Can modify classes and methods (final method will be introduced later)
5. Here is a class with final
Define a final class FinalClass, including a public function
1 2 3 4 5 |
|
Define a class ChildFinalClass and inherit the FinalClass class
1 2 3 4 5 |
|
In this way, when the above command is executed, the system will prompt
1 |
|
It proves that the class defined by the final keyword cannot be inherited by subclasses
2. Definition of member attributes in a class
i. Member attributes are some variable attributes defined for the class. As a class, everyone has a pair of eyes (normal, (Except Erlang Shen), one mouth, two ears, etc. Some fixed proper nouns used to describe or express something are called member attributes
ii. The key to using when declaring member attributes in a class Word
iii. Common member attribute declarations are composed of the following keywords
public, var, protected, private, and then followed by a variable. There are also some member attributes including static, constant const.
Public: Indicates global, and can be accessed by subclasses inside and outside the class
Var: This member attribute will be considered in the PHP 5 version Attributes of public type
protected means protected and can only be accessed by this class or subclass or parent class
private means private and can only be used within this class
Static: 1) Static properties,
2) Member properties modified with static can be shared by all objects of the same class
3) Static data It exists in the data segment in the memory when the class is loaded for the first time (initializing the static segment)
4) Use self::member attribute name
in the class External class name:: member attribute name
Const: 1) Constant attributes in the class. When declaring constants in the class, you must use const
2) Use self:: member attribute name in the class
Class name:: member attribute name
Note: The variables in the attributes can be initialized, but the initialized value must be a constant. The constant here refers to the PHP script during the compilation phase It is a constant, not a constant calculated in the runtime after the compile stage. For example, it cannot contain any operators, cannot be any variables, cannot be a function, etc.
iv. How to call member properties:
In the member method of the class, you can use $this->property (property is the property name). To access the properties and methods of a class, but it cannot be used to access the static properties of a class or in a static method. Instead, use self::$property. The pseudo variable $this can be used in non-static methods of a class. This pseudo variable is a reference to the instantiated object that calls the method.
Next, use code to explain the above content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
3. Definition of operations in the class
i. I generally like to call operations as member methods. Below I will call operations methods, but they are all the same
ii. Definition of member methods: Member methods are some function methods defined for the class. For example, take this class as an example, people can eat, can If you can run and type code, this is a member method. That is to say, you can do some executable actions, which we understand as member methods
iii. For access to member methods and member attributes, please refer to the introduction to access to member attributes above.
iv. Member methods and member attributes also include public, protected, private, static, final and the scope is the same. Here are some examples for your reference and understanding.
v. Static member methods can only access static member properties and member methods, and you can use self::static method() to access static methods inside the class, and use class name::static method() to access the external class. )
1. Custom methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
2. Magic methods
i. Magic methods must be defined as public, as must all other magic methods
ii. From PHP 5 and later, classes in PHP can use magic methods. It stipulates that methods starting with two underscores () are reserved as magic methods, so it is recommended that everyone’s function names should not start with them, unless it is to overload existing magic methods. Next, some magic methods are listed. If you want to elaborate To understand, you can query and understand a certain one, so I won’t introduce it in detail here.
1. construct() Construction method
destruct()
2 , clone()
If you want to copy an object, you need to use the clone method
3. The toString()
method is automatically called when converting an object into a string. For example, when using echo to print an object,
4, sleep(), use
wakeup when serializing, and call
5, set_state()## when deserializing.
#When var_export() is called, this static method will be called (valid since PHP 5.1.0)6. invoke(valid in PHP 5.3.0 or above) When trying to invoke an object as a function, the invoke method is automatically called. 7. callStatic (valid for PHP 5.3.0 and above) is to handle static method calls 8. get() This method will be triggered when an undefined property is called. The parameter passed is the name of the property being accessed. set() When assigning a value to an undefined property, this method will be triggered, and the parameters passed are the property name and value to be set. The non-declaration here includes attributes whose access control is protected and private (that is, attributes that do not have permission to access) when called using an object. 9. isset() This method is called when the isset() function is called on an undefined property unset() When the unset() function is called on an undefined property This method is called when 10. call($method, $arg_array)This method is called when an undefined method is calledThe undefined methods here include A method without permission to access; if the method does not exist, go to the parent class to find the method. If it does not exist in the parent class, call the call() method of this class. If the call() method does not exist in this class, search for it. The call() method in the parent class. 11. autoload()Automatic loadingMagic method
The above is the detailed content of How to define classes and their member properties in PHP. For more information, please follow other related articles on the PHP Chinese website!