Home  >  Article  >  Backend Development  >  What does php object instantiation mean?

What does php object instantiation mean?

青灯夜游
青灯夜游Original
2022-04-11 19:40:522735browse

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.

What does php object instantiation mean?

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:

  • Variable name: the reference name of an object created by the class, you can access the members of the object through this name;

  • new: keyword, indicating that a new object is to be created;

  • Class name: indicating 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 a default constructor without parameters.

Due to the characteristics of object data encapsulation, object attributes (variables defined in the class) cannot be directly accessed by the main program block. The objects defined in the class must be called through the object. Attributes and behavior functions indirectly achieve the purpose of accessing data in the control class.

Example:


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

Objects actually exist and occupy dynamic resources.

A class is a blueprint of an object and may occupy static resources.

Object attributes occupy dynamic resources

Class (static) attributes are actually "global variables" on the class namespace

Performance considerations:

Each object occupies separate data space

The increased call level may consume execution time

Recommendation: "

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!

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