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.
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 <?php... ?> 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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function