Home >Backend Development >PHP Problem >What does php member variable mean?

What does php member variable mean?

青灯夜游
青灯夜游Original
2022-04-11 19:21:532915browse

In PHP, member variables refer to variables declared directly in the class. The declaration syntax is "access permission modifier variable name = variable value;"; the value of the member variable is not visible to the outside world, but can be accessed through the member function After the class is instantiated as an object, the variable can become a property of the object. Multiple variables can be declared in a class, that is, there can be multiple member variables in an object.

What does php member variable mean?

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

In PHP, you can use the class keyword to add Define a class in the form of a class name, and then use curly brackets { } to wrap the attributes and methods of the class defined in the class body. The syntax format of the class is as follows:

[修饰类的关键字] class 类名{
    类的属性和方法;
}

Class name, variable name and function name The naming rules are similar, and they all need to follow the custom naming rules in PHP, which can be any legal tag that is not a PHP reserved word. A legal class name starts with a letter or underscore, followed by a number of 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 should have a certain meaning and should not be composed of just a few letters.

Member variables

Variables declared directly in the class are called member variables (can also be called member attributes). The value of this variable is invisible to the outside world, but can be accessed through member functions. After the class is instantiated as an object, the variable can become an attribute of the object.

You can declare multiple variables in a class, that is, there can be multiple member variables in the object, and each variable stores different attribute information of the object. The syntax format is as follows:

访问权限修饰符 变量名称 = 变量值;

The type of member variables can be scalar types and composite types in PHP, so they can also be objects instantiated by other classes, but it is meaningless to use resources and empty types in classes .

As we introduced before, no keyword modification is required when declaring a variable. However, when declaring a member variable in a class, a keyword must be used in front of the variable to modify it, such as public, private, static, etc., but The variables modified by these keywords have certain meanings. If you do not need any modification with specific meaning, you can use the "var" keyword. Once the member variable is modified with other keywords, you need to remove "var".

Commonly used access permission modifiers and their meanings are as follows:

  • public: public, can be used inside the class, in subclasses, or outside the class , unrestricted;

  • protected: protected, can be used inside the class and subclasses, but cannot be used outside the 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, all the content between a pair of curly brackets must be in a piece of code, that is, a 735168f1c7a01f249edf3c181a7ba447 Between , it cannot be divided into multiple blocks.

[Example] Create a Students class and declare some member variables in the class. The code is as follows:

<?php
    class Students{
        var $name;
        public $age;
        private $sex;
        public static $school;
    }
?>

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

Tip: Permission modifiers can be mixed with the keyword static to define static variables, as shown in the code above.

Recommended: "PHP Video Tutorial"

The above is the detailed content of What does php member variable mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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