


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!

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool