Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to instantiate objects and access object members in PHP

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report