" to call: $object->attribute/function, provided that the variable/method is accessible. 2. Directly call class methods/variables: class::attribute/function, whether static or non-static. But there are prerequisites: A. If it is a variable, the variable needs to be accessible."/> " to call: $object->attribute/function, provided that the variable/method is accessible. 2. Directly call class methods/variables: class::attribute/function, whether static or non-static. But there are prerequisites: A. If it is a variable, the variable needs to be accessible.">

Home  >  Article  >  Backend Development  >  Basic usage of static variables in php

Basic usage of static variables in php

怪我咯
怪我咯Original
2017-07-16 11:24:162056browse

static is to define a staticobject or static variable, about static definition We will find out what characteristics a variable or class method has after reading the relevant examples in this article.

1. Create an object$object = new Class(), and then use "->" to call: $object->attribute/function , provided the variable/method is accessible.
2. Directly call class methods/variables: class::attribute/function, whether it is static or non-static. But there are prerequisites:
A. If it is a variable, the variable needs to be accessible.
B. If it is a method, in addition to the method being accessible, it also needs to meet:
b1) If it is a static method, there are no special conditions;
b2) If it is a non-static method, it needs to be changed if it is not used in the method. $this means that non-static variables/methods are not called. Of course, there is no problem with calling static variables/methods.

Then let’s take a look at the difference between using $object->… and using class::…:
1. When using $object->…, you need to execute the constructorCreate objects;
2. Use class::... to call static methods/variables, and there is no need to execute the constructor to create objects;
3. Use class::... to call non-static methods/variables, and there is no need to execute them. Constructors create objects.

Then the strange thing comes out. Since 2 and 3 are the same, what is the point of the existence of static methods/variables?
static static
Declaring a class member or method as static allows direct access without instantiating the class. Static members (except static methods) cannot be accessed through an object. Static members belong to the class and do not belong to any object instance, but object instances of the class can be shared.

The code is as follows:

class Child{ 

public $name; 
//这里定义并初始化一个静态变量 $nums 
public static $nums=0; 
function construct($name){ 

$this->name=$name; 
} 

public function join_game(){ 

//self::$nums 使用静态变量 
self::$nums+=1; 

echo $this->name."加入堆雪人游戏"; 

} 


} 

//创建三个小孩 

$child1=new Child("李逵"); 
$child1->join_game(); 
$child2=new Child("张飞"); 
$child2->join_game(); 
$child3=new Child("唐僧"); 
$child3->join_game(); 

//看看有多少人玩游戏 
echo "<br/> 有这".Child::$nums;


The above is the detailed content of Basic usage of static 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