Home >Backend Development >PHP Problem >What is the definition and instantiation method of php class
In PHP, you can use the class keyword plus the class name to define a class. The syntax is "[keywords that modify the class] class class name {class attributes and methods;}"; you can use new Keyword to instantiate a class into an object, the syntax is "variable name = new class name (parameter list)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Definition of php class
In PHP, you can use the class keyword plus the class name to define a class, and then use curly brackets { } to wrap the attributes and methods of the class defined in the class body. The syntax format of the class is as follows:
[修饰类的关键字] class 类名{ 类的属性和方法; }
The naming rules for class names, variable names and function names are similar. They all need to follow the custom naming rules in PHP and can be any legal tag that is not a PHP reserved word. A legal class name starts with a letter or underscore, followed by a number of letters, numbers, or underscores. If the class name consists of multiple words, it is customary to capitalize the first letter of each word. In addition, the class name should have a certain meaning and should not be composed of just a few letters.
The keyword that modifies the class is an optional parameter and can be omitted. We usually use the following keywords to modify classes:
abstract: Abstract class or method. After being modified as an abstract class, the class cannot be instantiated, but can be inherited. If at least one method in a class is declared abstract, then the class must also be declared abstract. When inheriting an abstract class, the subclass must redefine all abstract methods in the parent class, and the access control of these methods must be the same as in the parent class.
final: Classes decorated with final cannot be inherited, and methods decorated with final cannot be redefined in subclasses.
Note: A class can contain its own constants, variables (called "member properties" or "properties" in the class) and functions (called "members" in the class method" or "method").
For example:
Define a car class with attributes including the color and price of the car
class car{ public $color; //定义属性 public $price; }
Instantiation of the php class
The instantiation of a class is also called creating an object or instantiating an object or instantiating a class.
It is very easy to instantiate a class into an object. Just use the new keyword and follow it with a method with the same name as the class name. Of course, if you do not need to pass parameters for the object when instantiating the object, just use the class name directly after the new keyword without adding parentheses.
The instantiation format of the object is as follows:
变量名 = new 类名(参数数列表); 或 变量名 = new 类名();
The parameter description is as follows:
Variable name: the reference name of an object created through the class, The members of the object can be accessed through this name;
new: keyword, indicating that a new object is to be created;
Class name: Represents the type of new object;
Parameter list: The constructor of the specified class is used to initialize the value of the object. If there is no constructor defined in the class, PHP will automatically create one without parameters. 's default constructor. (We will introduce it in detail later).
A simple example:
We define a human class and instantiate this class.
class Preson { //定义了一个Preson类 public $name; //定义类的属性(姓名,性别,年龄等等) public $age; public $gender; // public ..... } //new翻译是新的,意思就是创建一个新的人,并把这个新的对象赋值给$Preson1,这个就是实例化 $Preson1 = new Preson(); //实例化类 $Preson1->name = "张三"; $Preson1->age = 22; $Preson1->gender = "女"; echo $Preson1->name.' '.$Preson1->age.' '.$Preson1->gender; //如果想输出第二个实例,直接输出就OK了,只要改一个变量名 $Preson2 = new Preson(); //实例化类 $Preson2->name = "小亮"; $Preson2->age = 25; $Preson2->gender = "男";
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the definition and instantiation method of php class. For more information, please follow other related articles on the PHP Chinese website!