Get the App
SLTechnology News&Howtos  ›  Development  › 

How to instantiate objects and access object members in PHP

Shulou Source: shulou.com Published: 2022-06-03 01:50:15 09月10日 Update

This article introduces the knowledge of "how to instantiate objects and access object members in PHP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Earlier, we introduced how to define a class. It's easy to use a variable and function, but if you want to use the properties and methods in the class, you have to instantiate the class first. Let's take a look.

Instantiate object

To instantiate a class into an object, you just need to use the new keyword and add a method with the same name as the class name. Indicates a new object.

It is important to note that when an instantiated object does not need to pass parameters to the object, use the class name directly after the new keyword without parentheses.

The syntax format of the instantiated object is as follows:

Variable name = new class name (parameter list), or variable name = new class name

What we need to pay attention to in the syntax format is:

The variable name is the reference name of an object created by the class, through which the members of the object can be accessed.

Parameter list specifies that the constructor of the class is used to initialize the value of the object. If there is no defined constructor in the class, PHP automatically creates a default constructor without arguments.

Next, let's take a look at how to instantiate an object through an example, as follows:

Output result:

A class can instantiate multiple objects, each of which is independent. The above code instantiates three objects through an instance of the family class.

Access to members of an object

The object is made up of member properties and member methods. If you want to access the members of the object, it is actually a bit like accessing the elements in the array. Since it is somewhat similar, you need to access the members of the object through the reference of the object. You also need to use a special operation symbol-> to complete the access to the members of the object.

Its syntax format is as follows:

Variable name = new class name (parameter); / / instantiate a class variable name-> member attribute = value; / / assign variable name to member attribute-> member attribute; / / directly get value variable name of member attribute-> member method (); / / access member method in object

The method used is simple, let's take a look at an example:

Output result:

That's all for "how to instantiate objects and access object members in PHP". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Objects members instances variables methods properties parameters class names and then functions formats syntax output keywords contents names actually that is more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info Redmi Apple Shulou Information