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

What is the method to instantiate an object in php

青灯夜游
Release: 2023-03-11 11:52:02
Original
3145 people have browsed it

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 类名;
Copy after login

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);
?>
Copy after login

The running results are as follows:

object(Students)#1 (0) { }
object(Students)#2 (0) { }
object(Students)#3 (0) { }
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template