「php」クラスにstaticメソッドとstatic変数を利用するサンプル
関数説明
public static function 関数名{
static 変数名
}
PHPコード
<?php
class SampleA
{
public static function testFunc()
{
//static変数x
static $x = 6;
$x++;
return $x;
}
}
//クラス名のstatic関数を呼び出す
$result1="1 time: ".SampleA::testFunc()."<br>\n";
echo $result1;
$result2="2 time: ".SampleA::testFunc()."<br>\n";
echo $result2;
$result3="3 time: ".SampleA::testFunc()."<br>\n";
echo $result3;
?>
<?php
class SampleA
{
public static function testFunc()
{
//static変数x
static $x = 6;
$x++;
return $x;
}
}
//クラス名のstatic関数を呼び出す
$result1="1 time: ".SampleA::testFunc()."<br>\n";
echo $result1;
$result2="2 time: ".SampleA::testFunc()."<br>\n";
echo $result2;
$result3="3 time: ".SampleA::testFunc()."<br>\n";
echo $result3;
?>
<?php class SampleA { public static function testFunc() { //static変数x static $x = 6; $x++; return $x; } } //クラス名のstatic関数を呼び出す $result1="1 time: ".SampleA::testFunc()."<br>\n"; echo $result1; $result2="2 time: ".SampleA::testFunc()."<br>\n"; echo $result2; $result3="3 time: ".SampleA::testFunc()."<br>\n"; echo $result3; ?>
実行結果
1 time: 7
2 time: 8
3 time: 9