PHP/Language Basics/static variables — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 10:37, 26 мая 2010
Содержание
A static variable remembering its last value
<?php
function birthday( ){
static $age = 0;
$age = $age + 1;
echo "Birthday number $age<br />";
}
$age = 30;
birthday( );
birthday( );
echo "Age: $age<br />";
?>
static variables
<?php
function keep_track() {
STATIC $count = 0;
$count++;
print $count;
print "<br>";
}
keep_track();
keep_track();
keep_track();
?>
Using the static modifier to change a member or method so it is accessible without instantiating the class
<?php
class myclass {
const MYCONST = 123;
static $value = 567;
}
echo "myclass::MYCONST = " . myclass::MYCONST . "\n";
echo "myclass::$value = " . myclass::$value . "\n";
?>
Working with Static Variables in Functions
<?php
function statictest() {
static $count = 0;
$count++;
return $count;
}
statictest();
statictest();
$foo = statictest();
echo "The statictest() function ran $foo times.<BR>";
?>