Home >Backend Development >PHP Tutorial >Questions about php static variables

Questions about php static variables

怪我咯
怪我咯Original
2017-07-10 11:39:541330browse

Members of phpVariables can be initialized at the same time as they are declared, but they can only be initialized with scalars.

For example:

class A { 
public $f1 = 'xxxx'; 
static public $f2 = 100; 
}

If you want to assign a variable to an object, you can only initialize it in the constructor, for example:

class A { 
private $child; 
public function construct() { 
$this->child = new B(); 
} 
}

But there is nothing similar to the static constructor/static block in php in php, so there is no appropriate time to initialize it.

There are ways to solve the problem for shared members, for example:

class A { 
static public $child; 
} 
A::$child = new B();

There seems to be no clean method for private members, we can only do this:

class A { 
static private $child; 
static public initialize() { 
self::$child = new B(); 
} 
} 
A::initialize();

Another important feature of variable scope in php is static variables (static variables). Static variables only exist in the local function domain and are only initialized once. When the program execution leaves this scope, its value will not disappear, and the result of the last execution will be used.

Look at the following example:

<?php 
function Test() 
{ 
$w3sky = 0; 
echo $w3sky; 
$w3sky++; 
} 
?>

This function will set the value of $w3sky to 0 and output "0" every time it is called. Increasing the variable $w3sky++ by one has no effect, because the variable $w3sky does not exist once this function exits. To write a counting function that will not lose this count value, define the variable $w3sky as static:
As follows:

<?php 
function Test() 
{ 
static $w3sky = 0; 
echo $w3sky; 
$w3sky++; 
} 
?>

This function will output $w3sky every time Test() is called value and add one.
Static variables also provide a way to deal with recursive functions. A recursive function is a method that calls itself. Be careful when writing recursive functions, as they may recurse indefinitely without an exit. Be sure to have a way to abort the recursion. The following simple function recursively counts to 10, using the static variable $count to determine when to stop:
Example of static variables and recursive functions:

<?PHP 
function Test() 
{ 
static $count = 0; 
$count++; 
echo $count; 
if ($count < 10) { 
Test(); 
} 
$count--; 
} 
?>

Note: Static variables can be declared as in the above example. If it is assigned with the result of expression in the declaration, it will cause a parsing error.
Example of declaring static variables:

<?PHP 
function foo(){ 
static $int = 0;// correct 
static $int = 1+2; // wrong (as it is an expression) 
static $int = sqrt(121); // wrong (as it is an expression too) 
$int++; 
echo $int; 
} 
?>

The above is the detailed content of Questions about php static variables. 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