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 is the php member variable

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what are the member variables of php". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are php member variables?"

In php, the member variable refers to the variable declared directly in the class, declaring the syntax "access modifier variable name = variable value;"; the value of the member variable is invisible, but can be accessed through the member function, and after the class is instantiated as an object, the variable can become the property of the object. Multiple variables can be declared in a class, that is, there can be multiple member variables in an object.

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

In PHP, you can define a class using the class keyword plus the class name, and then wrap the properties and methods that define the class in the class body with curly braces {}. The syntax format of the class is as follows:

[modifier class keywords] class class name {properties and methods of the class;}

Class names are similar to variable names and function names by following the custom naming rules in PHP, which can be legal tags for any non-PHP reserved word. A legal class name begins with a letter or underscore, followed by several letters, numbers, or underscores. If the class name consists of multiple words, it is customary to capitalize the first letter of each word. In addition, the class name had better have a certain meaning, not just a few letters.

Member variable

Variables that are declared directly in a class are called member variables (also known as member properties). The value of the variable is invisible, but it can be accessed through a member function, and after the class is instantiated as an object, the variable becomes a property of the object.

You can declare multiple variables in a class, that is, an object can have multiple member variables, each of which stores different attribute information for the object. The syntax format is as follows:

Access modifier variable name = variable value

The types of member variables 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 declaring a variable, but when you declare a member variable 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 do not need a specific modification, you can use the "var" keyword, and once the member variable has other keyword modifications, you need to remove "var".

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 variables in the class, as shown in the following code:

$name, $age$sex, and $school in the above example are all member variables.

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

At this point, I believe you have a deeper understanding of "what php member variables are". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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