Questions about php static variables

怪我咯
Release: 2023-03-13 11:12:02
Original
1246 people have browsed it

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; 
}
Copy after login

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(); 
} 
}
Copy after login

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();
Copy after login

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();
Copy after login

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++; 
} 
?>
Copy after login

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++; 
} 
?>
Copy after login

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--; 
} 
?>
Copy after login

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; 
} 
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!