


What is static in PHP? Learn more about static properties and static methods
This article will take you to understand the static attributes and static methods in PHP, introduce the nature of static, and the difference between static variables and ordinary variables. I hope it will be helpful to everyone.
What is static?
We mentioned before that calling member variables and methods requires the use of objects. But if We don’t want to instantiate, what if we directly access variables and methods? This uses the static function.
When defining variables and methods, add the static keyword in front of them to convert them into static, and you can pass Class name::double quotes for direct access.
We continue to rewrite the above example into static variables and static methods:
class Classname //定义一个类, 类名不区分大小写 { static public $name; //定义静态成员变量; static public $height=180; //初始化赋值 static public $weight, $nationality; //可以一个语句定义多个变量 static protected $age; static public function player($name,$height, $weight, $age, $sex) { //定义静态成员方法, 以及方法形参 self::$name=$name; //为成员变量赋值, 使用了代词self:: self::$weight=$weight; self::$height=$height; if (self::$height<185 && self::$weight<=85){ return self::$name.',符合要求'; }else{ return self::$name.',不太行'; } } } echo Classname::$height; //180, 通过类名::访问静态变量 echo Classname::player('xiaoming',180,80,22, 'Male'); //通过类名::访问静态方法;
As you can notice in the above example, I changed all $this are replaced by self. Because $this refers to the calling object, and self refers to the class itself where the method is located. Static methods can be called directly through the class name::. There is no object here, and \$this refers to If it is empty, the system will report an error. Therefore, when calling variables in a static method, you cannot pass $this.
Note that the variable following self:: must have the $ symbol.
The nature of static
Don’t underestimate this static. With it, it is not just that you can call it directly. The meaning of member variables and methods has fundamentally changed:
In Only static variables can be called in static methods, but ordinary variables cannot be called. Ordinary methods can call static variables. This is determined by the attributes of the static method, because ordinary member variables are bound to "objects". Static variables are bound to "classes".
I will explain the difference between static variables and ordinary variables in detail:
Ordinary The member variables are bound to the object. Different objects have their own set of member variables. The member variables of different objects have their own assignments. Although they may be the same, yours is yours.
Static variables are bound to the class. If the static variable changes, then the value will change in all objects of this class.
Static variables can also be used Access is through object::, but in fact, objects of the same class access the same static variable value. It can be understood that static variables are shared by the entire class, including its subclasses.
So even if one of the objects is destroyed, the static variable value will still be retained.
Subclasses can also override the static member variables of the parent class, but the parent class’s The static variables still exist, and these two static member variables are independent. They will be accessed separately according to the calling class name.
Let’s give an example:
class Shouji { static public $test; //定义一个静态变量 static function test5() //定义静态方法来操作并输出静态变量 { self::$test++; echo self::$test; } } class Shouji2 extends Shouji //定义一个子类 { static function test5() //定义子类的静态方法 { self::$test++; //访问并操作父类的静态变量 echo self::$test; } } $shouji1=new Shouji; //新建父类对象 $shouji1->test5(); //1, 通过对象调用静态方法 $shouji2=new Shouji; //新建另一个父类对象 $shouji2->test5(); //2, 在前一次操作基础上继续+1 $shouji3=new Shouji2; //新建子类对象 $shouji3->test5(); //3, 调用子类同名静态方法, 继续+1 echo Shouji::$test; //3, 通过父类::直接访问静态成员变量 echo $shouji1::$test; //3, 通过对象名::可以直接访问静态成员变量
Pass In the above example, we can also summarize the following points:
The subclass can override the static method of the parent class.
In the method To access static variables, you need to use the :: symbol. You cannot use $this;
Static methods and static variables will be inherited by subclasses.
Static variables It cannot be accessed by ->, but by :: (double colon)
The object can directly call static member methods through the object name ->, which is the same as calling ordinary methods It’s the same.
This article is reproduced from: https://juejin.cn/post/6977200691919978510
Author: The old man in the communication room
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is static in PHP? Learn more about static properties and static methods. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
