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

What does a method or property of a php object mean?

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

Share

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

This article mainly introduces the php object method or property means what the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that you read this php object method or property means what the article will have a harvest, let's take a look at it.

In the php object, the method refers to the function created in the class structure, which implements a behavior in the class and is a part of the class, while the attribute refers to the variable declared directly in the class structure. There can be multiple attributes in the object, and each variable stores different attribute information of the object.

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

First of all, let's take a brief look at the relevant concepts:

Class: class, which defines the outermost structure of the object-oriented subject, and is also used to wrap the subject data and functions (functions). Class is the representative of a class of common transactions, which represents the commonness of transactions.

Object: object, which is not only the concrete representative of some kind of transaction, but also the concrete unit of actual data and functional operation, also known as instance.

Instantiation: new, the process of obtaining a concrete instance from an abstract concept that conforms to the abstract concept.

Class member: member, which refers to all the content in the class class structure. There are three types of class members.

Method: method, essentially a function created in a class-like structure, also known as a member method, or a member function.

Attribute: property, which is essentially a variable created in a class-like structure, also known as a member variable.

Let's focus on properties and methods.

Member attribute

Variables declared directly in a class are called member attributes (also known as member variables). Multiple variables can be declared in a class, that is, there can be multiple member properties in an object, and each variable stores different attribute information for the object. The syntax format is as follows:

Access modifier attribute name = attribute value

The types of member attributes can be scalar types and compound types in PHP, so they can also be objects instantiated by other classes, but it doesn't make sense to use resources and empty types in classes.

As we explained earlier, you don't need any keyword modification when you declare a variable, but when you declare member attributes in a class, you must use a keyword in front of the variable, such as public, private,static, etc., but the variables modified by these keywords have a certain meaning. If you don't need a meaningful modifier, you can use the "var" keyword, and you need to remove "var" once the member property has other keyword modifiers.

Common access modifiers and their meanings are as follows:

Public: public, can be used inside a class, in a subclass, or outside a class, without restriction

Protected: protected, can be used inside and subclasses of a class, but not outside of a class

Private: private, can only be used inside the class, and cannot be used outside the class or in subclasses.

Note: a class, that is, everything between a pair of curly braces, should be in a piece of code, that is, between one, and cannot be divided into multiple pieces.

[example] create a Students class and declare some member properties in the class. The code is as follows:

Tip: permission modifiers can be mixed with the keyword static that defines static variables, as shown in the code above.

Member method

Functions defined in a class are called member methods. The only difference between a function and a member method is that the function implements an independent function, while the member method is a behavior in the implementation class and is part of the class.

You can declare multiple member methods in a class, and the declaration of a member method is exactly the same as that of a function, except that you can control access by adding some access modifiers before the function keyword, such as public, private, protected, and so on.

It is also important to note that declared member methods must be related to the class and cannot be meaningless operations. For example, when declaring a student class, if you declare the member method of "fly", every student instantiated can fly, which is a design error.

[example] create some member methods in the Students class created in the above example.

The permission modifier in front of the member method can be omitted, and the default permission is public when omitted. The declaration of member attributes and member methods in the class is optional and can exist at the same time or separately, depending on the actual situation.

In PHP7, type declarations are introduced, and we can declare types for the formal parameters and return values of member methods in the following format:

[permission modifier] function method name (type parameter 1, type parameter 2,..., type parameter n): return value type {.}

The parameter types that are supported in PHP7 are integer, floating-point, string, and Boolean types. The sample code is as follows:

Instantiate the object and access the members of the object

The object contains member properties and member methods, and accessing the members of the object is similar to the elements in the access array, and the members of the object can only be accessed through the object's reference. However, a special operation symbol-> is also used to complete the access to the members of the object, and the syntax format of the members of the access object 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

Here is an example to demonstrate:

This is the end of the article on "what does a method or property of a php object mean?" Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what does the method or property of php object mean". If you want to learn more, you are welcome to follow the industry information channel.

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