Home > Article > Backend Development > How to define classes and their member properties in PHP
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
Class Person{};
2. Define an Classes with member attributes and operations
Class Person{ 成员属性........ 操 作........ }
3. To define a class that cannot be inherited, use the final keyword
Final class Person{ 成员属性........ 操 作........ }
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
final class FinalClass { public function ffun() { echo “本类为final类”; } }
Define a class ChildFinalClass and inherit the FinalClass class
class ChildFinalClass extends FinalClass { public function fchildfun() { echo ‘本类继承final类FinalClass ’; } }
In this way, when the above command is executed, the system will prompt
Fatal error: Class ChildFinalClass may not inherit from final class (FinalClass)
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:
class FinalClass { //static $a = strTolower(); //这种写法错误 //const A = 1+2; //这种写法错误 //public $name = 123+456; //这种写法错误 static $a = '$a'; const A = 'A'; public $name = '凤姐'; protected $sex = '男+女'; private $age = 23; } class ChildFinalClass extends FinalClass{ public function fchildfun(){ echo 'ChildFinalClass类中ChildFinalClass::$a->'.ChildFinalClass::$a."<hr>"; Echo 'ChildFinalClass类中ChildFinalClass::A->'.ChildFinalClass::A."<hr>"; echo '大家好,我叫'.$this->name."<hr>"; echo '我是:'.$this->sex.'生<hr>'; echo '我今年:'.$this->age.'<hr>'; //由于age是私有的成员属性,所以在这里将不会被调用,将提示没有定义此属性在ChildFinalClass类中。 } } $obj = new ChildFinalClass(); $obj->fchildfun();
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:
class FinalClass { static $a = '$a'; const A = 'A'; public $name = '凤姐'; protected $sex = '男+女'; private $age = 23; //定义一个公共方法 public function publickfun(){ echo 'FinalClass类中self::$a->'.self::$a."<hr>"; echo "FinalClass类中self::A->".self::A."<hr>"; echo '大家好,我叫:'.$this->name."<hr>"; echo '我是:'.$this->sex.'生<hr>'; echo '我今年:'.$this->age.'<hr>'; } //定义一个受保护的方法 protected function protectedfun(){ echo 'FinalClass类中self::$a->'.self::$a."<hr>"; echo "FinalClass类中self::A->".self::A."<hr>"; echo '大家好,我叫:'.$this->name."<hr>"; echo '我是:'.$this->sex.'生<hr>'; echo '我今年:'.$this->age.'<hr>'; } //定义一个私用方法 private function privatefun(){ echo 'FinalClass类中self::$a->'.self::$a."<hr>"; echo "FinalClass类中self::A->".self::A."<hr>"; echo '大家好,我叫:'.$this->name."<hr>"; echo '我是:'.$this->sex.'生<hr>'; echo '我今年:'.$this->age.'<hr>'; } } class ChildFinalClass extends FinalClass{ public function fchildfun(){ echo 'ChildFinalClass类中ChildFinalClass::$a->'.ChildFinalClass::$a."<hr>"; Echo 'ChildFinalClass类中ChildFinalClass::A->'.ChildFinalClass::A."<hr>"; echo '我是:'.$this->sex.'生<hr>'; echo '我今年:'.$this->age.'<hr>'; //由于age是私有的成员属性,可以理解为我不想让别人知道我的年龄,所以在这里将不会被调用,将提示没有定义此属性在ChildFinalClass类中。 $this->publickfun(); $this->protectedfun(); $this->privatefun(); //由于privatefun是私有的成员方法,所以在这里将不会被调用。 } } $obj = new ChildFinalClass(); $obj->fchildfun();
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!