Home  >  Article  >  Backend Development  >  What is the method to instantiate an object in php

What is the method to instantiate an object in php

青灯夜游
青灯夜游Original
2021-07-09 17:41:033033browse

How to instantiate an object in php: Just use the new keyword and follow it with a method with the same name as the class name. The syntax format is "variable name = new class name (parameter list);" Or "Variable name = new class name;".

What is the method to instantiate an object in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Instantiated objects

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: Indicates 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).

[Example] Create a class and instantiate it.

<?php
    class Students{
    }
    $person1 = new Students();
    $person2 = new Students;
    $person3 = new Students;
    var_dump($person1);
    echo &#39;<br>&#39;;
    var_dump($person2);
    echo &#39;<br>&#39;;
    var_dump($person3);
?>

The running results are as follows:

object(Students)#1 (0) { }
object(Students)#2 (0) { }
object(Students)#3 (0) { }

A class can instantiate multiple objects, and each object is independent. In the above code, three objects $person1, $person2 and $person3 are instantiated through the Students class, which is equivalent to opening up three spaces in the memory to store each object.

There is no connection between multiple objects declared using the same class. It can only mean that they are all of the same type. Each object has member attributes and member methods declared in the class. Just like three independent people, each has its own name, gender and age attributes, and each one has its own way of talking, eating and walking.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the method to instantiate an object 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