Static variables are also called class variables, and static methods are also called class methods.
Simply put, a static variable is a variable that all objects share. Static variables are also called class variables, and static methods are also called class methods.
How to define a static variable
Access modifier static variable name;
How to access static variables:
(1) outside the class: class Name:: $ class variable name
(2) inside the class:self::$ class variable name or:: $ class variable name
A group of children are playing games and new children are added to the game. Q: How to know how many children are playing games now, using object-oriented thinking.
The difference between a static variable and a workaround variable:
(1) Coupled with static is called statically variable or class variable, otherwise called instance variable;
(2) class variables are related to the class, the public properties;
(3) The instance variable belongs to the individual attribute of each object;
(4) class variables can be accessed directly by the class name:: Class variable name.
Mode one: Using global variables
What is a global variable? A variable that can be used anywhere in the program, called a global variable.
<?php
Global variables to play the number of games
Global $global _nums;
$global _nums=0;
Class child{
Public $name;
function __construct ($name) {
$this->name= $name;
}
Public Function Join_game () {
Declaration uses global variables
Global $global _nums;
$global _nums+=1;
echo $this->name. ' Join the game. ' <br/> ";
}
Create three children
}
$child 1=new Child ("www.bianceng.cn");
$child 1->join_game ();
$child 2=new Child ("Dick");
$child 2->join_game ();
$child 3=new Child ("King II");
$child 3->join_game ();
See how many people play games
echo "<br/> has". $global _nums. " People play games ";
?>
Mode two: Using static variables
<?php
Class child{
Public $name;
public static $nums = 0; Defining static variables
function __construct ($name) {
$this->name= $name;
}
Public Function Join_game () {
Self:: $nums +=1;
echo $this->name. ' Join the game. ' <br/> ";
}
Create three children
}
$child 1=new Child ("www.bianceng.cn");
$child 1->join_game ();
$child 2=new Child ("Dick");
$child 2->join_game ();
$child 3=new Child ("King II");
$child 3->join_game ();
See how many people play games
echo "<br/> has". Child:: $nums. " People play games ";
?>
PHP static method
Static methods are also called class methods, and static methods belong to all object instances in the form of the following:
Access modifier static method name () {
Statement
}
When we manipulate static variables, we can consider using static methods
Note: Non-static properties (variables) cannot be accessed in class methods, and normal methods can access either static variables or non-static variables.
Use of class methods:
Outside the class:
Class Name:: Class method Name or object name-> class method name;
Inside the class:
Self:: Class method Name or class Name: Class method Name
Example: The sum of the statistics of tuition
<?php
Class student{
public static $fee = 0;
Public $name;
Constructors
function __construct ($name) {
$this->name= $name;
}
Entrance
public static function Enterschool ($ifee) {
Self:: $fee + = $ifee;
}
Get the total tuition fee
public static function Getfee () {
Return self:: $fee;
}
}
Create a student
$stu 1=new Student ("John");
To invoke a static method by using the class name
Student::enterschool (340); Equivalent to $stu1->enterschool (340);
$stu 2=new Student ("Dick");
$stu 2->enterschool (600);
echo "Total tuition fee is:". Student::getfee ();
?>