What are php classes? Detailed explanation of attributes of php class

伊谢尔伦
Release: 2023-03-11 13:22:01
Original
17123 people have browsed it

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 the class uses class

1. Define an empty class

Class Person{};

2. Define a class with member attributes and operations

Class Person{

Member attributes...

Operations ........

}

3. Define a class that cannot be inherited, use the final keyword

Final class Person{

Member Attributes...

Operation...

}

4. Note: the final keyword cannot be used Modifying member attributes can only modify classes and methods (final methods 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类”;
}
}
Copy after login

Define a class ChildFinalClass and inherit the FinalClass class

class ChildFinalClass extends FinalClass {
public function fchildfun() {
echo ‘本类继承final类FinalClass ’;
}
}
Copy after login

In this way, when the above command is executed, the system will prompt

Fatal error: Class ChildFinalClass may not inherit from final class (FinalClass )

Prove that the class defined by the final keyword cannot be inherited by subclasses

2. Definition of member attributes in the class

i. Member attributes are some variable attributes defined for the class. As a class, people have a pair of eyes (normal, except for Erlang Shen), a mouth, two ears, and other fixed attributes used to describe Or a proper noun that expresses something is called a member attribute

ii. The keywords used to declare member attributes in a class

iii. Common member attribute declarations are made of the following keywords
It starts with public, var, protected, private, and is 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 exists in the data segment in memory when the class is loaded for the first time (initializing the static segment)

4) Use self:: member attribute name in the class

5) Outside the class Class name:: member attribute name

Const: 1) Constant attribute in the class, you must use const

when declaring constants in the class 2) Use self:: member attribute in the class Name

3) Use outside the class Class name::Member attribute name

Note: The variables in the attribute can be initialized, but the initialized value must be a constant. The constant here refers to the PHP script in It is a constant during the compilation phase, rather than a constant calculated during the runtime phase after the compilation phase. 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 &#39;ChildFinalClass类中ChildFinalClass::$a->&#39;.ChildFinalClass::$a."<hr>";
Echo &#39;ChildFinalClass类中ChildFinalClass::A->&#39;.ChildFinalClass::A."<hr>";
echo &#39;大家好,我叫&#39;.$this->name."<hr>";
echo &#39;我是:&#39;.$this->sex.&#39;生<hr>&#39;;
echo &#39;我今年:&#39;.$this->age.&#39;<hr>&#39;;   
//由于age是私有的成员属性,所以在这里将不会被调用,将提示没有定义此属性在ChildFinalClass类中。
 
}
}
$obj = new ChildFinalClass();
$obj->fchildfun();
Copy after login

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 method:

class FinalClass {
static $a = &#39;$a&#39;;
const  A = &#39;A&#39;;
public $name = &#39;凤姐&#39;;
protected $sex = &#39;男+女&#39;;
private $age = 23;
 
    //定义一个公共方法
public function publickfun(){
echo &#39;FinalClass类中self::$a->&#39;.self::$a."<hr>";
echo "FinalClass类中self::A->".self::A."<hr>";
echo &#39;大家好,我叫:&#39;.$this->name."<hr>";
echo &#39;我是:&#39;.$this->sex.&#39;生<hr>&#39;;
echo &#39;我今年:&#39;.$this->age.&#39;<hr>&#39;;
}
 
//定义一个受保护的方法
protected function protectedfun(){
echo &#39;FinalClass类中self::$a->&#39;.self::$a."<hr>";
echo "FinalClass类中self::A->".self::A."<hr>";
echo &#39;大家好,我叫:&#39;.$this->name."<hr>";
echo &#39;我是:&#39;.$this->sex.&#39;生<hr>&#39;;
echo &#39;我今年:&#39;.$this->age.&#39;<hr>&#39;;
}
    //定义一个私用方法
private function privatefun(){
echo &#39;FinalClass类中self::$a->&#39;.self::$a."<hr>";
echo "FinalClass类中self::A->".self::A."<hr>";
echo &#39;大家好,我叫:&#39;.$this->name."<hr>";
echo &#39;我是:&#39;.$this->sex.&#39;生<hr>&#39;;
echo &#39;我今年:&#39;.$this->age.&#39;<hr>&#39;;
}
}
class ChildFinalClass extends FinalClass{
public function fchildfun(){
echo &#39;ChildFinalClass类中ChildFinalClass::$a->&#39;.ChildFinalClass::$a."<hr>";
 
Echo &#39;ChildFinalClass类中ChildFinalClass::A->&#39;.ChildFinalClass::A."<hr>";
 
echo &#39;我是:&#39;.$this->sex.&#39;生<hr>&#39;;
 
echo &#39;我今年:&#39;.$this->age.&#39;<hr>&#39;;   
//由于age是私有的成员属性,可以理解为我不想让别人知道我的年龄,所以在这里将不会被调用,将提示没有定义此属性在ChildFinalClass类中。
 
$this->publickfun();
 
$this->protectedfun();
 
$this->privatefun();            
//由于privatefun是私有的成员方法,所以在这里将不会被调用。
}
}
$obj = new ChildFinalClass();
$obj->fchildfun();
Copy after login


2. Magic method

i. Magic method must be defined as public, all other magic methods must in this way

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()

Destruction method

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 objects,

4, sleep(), when serializing, use

wakeup, when deserializing, call

5, set_state()

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 When you call an object by calling 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. 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 called

The undefined method here includes no permission to access method; 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, go to the parent class. call() method.

11. autoload() Automatic loadingMagic method

The above is the detailed content of What are php classes? Detailed explanation of attributes of php class. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template