PHP/Language Basics/static variables
Содержание
A static variable remembering its last value
<source lang="html4strict">
<?php function birthday( ){
static $age = 0; $age = $age + 1; echo "Birthday number $age
";
}
$age = 30;
birthday( );
birthday( );
echo "Age: $age
";
?>
</source>
static variables
<source lang="html4strict">
<?php function keep_track() {
STATIC $count = 0; $count++; print $count; print "
";
} keep_track(); keep_track(); keep_track(); ?>
</source>
Using the static modifier to change a member or method so it is accessible without instantiating the class
<source lang="html4strict">
<?php class myclass {
const MYCONST = 123; static $value = 567;
} echo "myclass::MYCONST = " . myclass::MYCONST . "\n"; echo "myclass::$value = " . myclass::$value . "\n"; ?>
</source>
Working with Static Variables in Functions
<source lang="html4strict">
<?php
function statictest() { static $count = 0; $count++; return $count; } statictest(); statictest(); $foo = statictest(); echo "The statictest() function ran $foo times.
";
?>
</source>