Home  >  Article  >  Backend Development  >  What is the difference between variables and member variables in php

What is the difference between variables and member variables in php

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2023-06-16 14:45:581802browse

The difference between variables and member variables in php: 1. A variable is an identifier used to store a value or object, while member variables refer to attributes defined by a class or attributes of an object; 2. Variables can They are defined and used within statement blocks such as functions, conditional statements, and loops, but are not available outside the statement block. Member variables can be called by all methods in the class, even objects instantiated from the outside; 3. Variables are usually They are used to store temporary or data that needs to be changed during runtime, while member variables usually store object status information.

What is the difference between variables and member variables in php

Operating system for this tutorial: Windows 10 system, php8.1.3 version, Dell G3 computer.

1. The difference between variables and member variables in PHP:

Variables are declared and defined within functions or in the global scope; Member variables are defined Within a class, it exists as an attribute of the class.

So, one major difference is their different scopes. Variables can be defined and used within statement blocks such as functions, conditional statements, and loops, but they are not available outside statement blocks. However, member variables can be called by all methods within the class, even objects instantiated from outside.

2. Purpose of variables and member variables:

  • Variables are usually used to temporarily store values ​​or data results that need to be reused or modified.

  • Member variables usually contain class-specific information or status. Since it exists as a property of the class, each object can have a different value set. This is very useful in object-oriented programming.

Variable:

A variable is an identifier used to store a value or object. It is assigned via the assignment operator "=" and has visibility in scope. For local variables, their visibility is limited to the code block in which the variable is defined. Global variables do not have this restriction. However, in order to prevent naming conflicts, you should try to avoid defining variables with the same name in different scopes. Variables are often used to store data that is temporary or needs to be changed at runtime.

Example:

$name = 'Tom';
$age = 20;

Member variables:

Member variables refer to the properties defined by the class or the properties of the object, passed inside the class definition through $ accessed by this keyword. Member variables can be defined as public, private, or protected, depending on the access control level of the class to which they belong. Its scope includes the entire class. Even after the class instance is destroyed, the member variables still exist in the class definition. Member variables usually store object state information.

Example:

class Person {
    public $name;
    private $age;
    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }
}
$person1 = new Person('Tom', 20);
echo $person1->name; // 可以访问
echo $person1->age; // 无法访问,因为是私有成员变量

The above is the detailed content of What is the difference between variables and member variables in php. 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