Home  >  Article  >  Backend Development  >  Class operations in PHP

Class operations in PHP

不言
不言Original
2018-04-20 10:28:542929browse

The content of this article is about the operation of classes in PHP. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

1. Class declaration
<?php
    权限修饰符 class 类名{
    类的内容;
}
?>


Permission modifier can be omitted. At this time, the default modifier is public, which means that each property and method Items are accessible from both inside and outside the class;

Properties and methods declared with the keyword private are only accessible from Access inside the class;

The properties and methods declared with the keyword protected can only be accessed from inside the class, but"Subclasses" generated through "inheritance" can also access these properties and methods.

2. The content of the class
class Student
{
  public $name;/*类的成员属性,修饰关键词publlic、protected、private,如果没有特定的意义,仍然需要用var关键词修饰*/
  function GetIp(){
  //方法的内容;
}
$lili=new 类名称();//类的实例化
The method can also be modified with the keywords public, protected, and private.

The $this keyword exists in every member method of the class. It is a special object reference method. Which object the member method belongs to, the $this reference represents which object.

The operator "::" can access members in a class without any declared instance.

3. Accessing member attributes and methods in a class


When accessing member variables or methods in a PHP class, if If the referenced variable or method is declared as const (definition of constant) or static (declaration of static), then the operator must be used::,

On the contrary, if the referenced variable or method is not declared as const or static, then You must use the operator ->.
In addition, if you access a const or static variable or method from within the class, you must use the self-referenced self.
On the contrary, if you access a non-const or static variable or method from within the class, you must use Self-referential $this.
Conclusion: The functions of self and $this are very similar, but they are not the same. $this cannot refer to static members and constants. self is more like the class itself, and $this is more like the instance itself.

4. Construction method and destructor method

The construction method __construct() is automatically called when instantiating the object
Purpose: Can be used in initialization programs (can assign values ​​to member attributes or call member methods)
Syntax: [Modifier] function __construct(parameter list...){ }
Constructor method format:
[Modifier] function __construct([parameter]){
Program body
}

The destructor method __destruct() is automatically called when the object is destroyed, that is to say, it is automatically called after all the steps are executed. Call, that is to say, automatically call


用途:可以进行资源释放操作或文件的关闭操作
注意:如果类中没有构造方法\析构方法,PHP引擎会自动添加一个构造方法\析构方法,其参数列表为空,方法内容为空


5, class inheritance and final keyword
class 子类名称 extends 父类名称{
     //子类成员变量列表
  function 成员方法(){//子类成员方法
     //方法内容
  }
}
# when the program ends


##The parent class has the public properties and methods of its subclass. In addition to the public properties and methods of the parent class, the subclass also has its own unique properties and methods.

If a method in the parent class is declared final, the subclass cannot override the method, and the final keyword can terminate the inheritance of the class.

Final classes cannot have subclasses, and final methods cannot be overridden.

Related recommendations:

The difference between abstract classes and interfaces in php


The above is the detailed content of Class operations in PHP. For more information, please follow other related articles on the PHP Chinese website!

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