Home > Article > Backend Development > What does php object instantiation mean?
In PHP, object instantiation refers to instantiating a class into an object, that is, the process of creating an object with a class, which is a process from abstract to concrete; just use the new keyword and add an and after it. Methods with the same class name can be instantiated with the syntax "variable name = new class name (parameter list);". Do not pass parameters for the object, and the parameter list can be omitted.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, object instantiation It refers to the process of instantiating a class into an object, that is, using a class to create an object. It is the process of converting an abstract conceptual class into a concrete object of that type.
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.The instantiation format of the object is as follows:
变量名 = new 类名(参数列表);Of course, if you do not need to pass parameters for the object when instantiating the object, use it directly after the new keyword The class name is sufficient, no parentheses are needed.
变量名 = new 类名;The parameter description is as follows:
<?php //声明一个电话类Phone class Phone { //类中成员同上(略) } // 声明一个人类Person class Person { //类中成员同上(略) } //通过Person类实例化三个对象$person1、$person2、$person3 $person1 = new Person(); //创建第一个Person类对象,引用名为$person1 $person2 = new Person(); //创建第二个Person类对象,引用名为$person2 $person3 = new Person(); //创建第三个Person类对象,引用名为$person3 //通过Phone类实例化三个对象$phone1、$phone2、$phone3 $phone1 = new Phone(); //创建第一个Phone类对象,引用名为$phone1 $phone2 = new Phone(); //创建第二个Phone类对象,引用名为$phone2 $phone3 = new Phone(); //创建第三个Phone类对象,引用名为$phone3
Access to members in objects
The class contains two parts: member properties and member methods. We can use The "new" keyword creates an object, that is:$引用名 = new 类名(构造参数);Then we can use the special operator "->" to access member properties or member methods in the object. For example:
$引用名 = new 类名(构造参数); $引用名->成员属性=赋值; //对象属性赋值 echo $引用名->成员属性; //输出对象的属性 $引用名->成员方法(参数);//调用对象的方法If the members in the object are not static, then this is the only way to access them.
The relationship between objects and classes:
Performance considerations:
Each object occupies separate data spaceThe increased call level may consume execution timeRecommendation: "PHP Video Tutorial"
The above is the detailed content of What does php object instantiation mean?. For more information, please follow other related articles on the PHP Chinese website!