Home > Backend Development > PHP Problem > Is there a way to instantiate an object in php?

Is there a way to instantiate an object in php?

青灯夜游
Release: 2023-03-17 19:12:02
Original
1689 people have browsed it

There are methods to instantiate objects in php. To instantiate an object, you only need to use the new keyword followed by a method with the same name as the class name. The syntax is "variable name = new class name (parameter number list);"; if you do not need to be an object when instantiating the object To pass parameters, just use the class name directly after the new keyword without adding parentheses. The syntax is "variable name = new class name;".

Is there a way to instantiate an object in php?

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

Instancing objects

In PHP, object instantiation refers to instantiating a class into an object, that is, the process of 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. 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 by the class, you can access the members of the object through this name;

  • new: Keyword, indicating that you want to create a new Object;

  • 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 a default constructor without parameters.

[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

Is there a way to instantiate an object in php?

A class can instantiate multiple objects, 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 Is there a way 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