「PHP」クラスにstatic変数を利用するサンプル
構文
static 変数名
PHPコード
<?php
class SampleTest
{
public function cftFunc()
{
//static 変数名
static $x = 5;
$x++;
return $x+2;
}
}
//クラスのオブジェクト作成
$cftA = new SampleTest();
$cftB = new SampleTest();
$cftC= new SampleTest();
//クラスの関数を呼び出します
print "value1:".$cftA->cftFunc()."<br>\n";
print "value2:".$cftB->cftFunc()."<br>\n";
print "value3:".$cftC->cftFunc()."<br>\n";
?>
<?php
class SampleTest
{
public function cftFunc()
{
//static 変数名
static $x = 5;
$x++;
return $x+2;
}
}
//クラスのオブジェクト作成
$cftA = new SampleTest();
$cftB = new SampleTest();
$cftC= new SampleTest();
//クラスの関数を呼び出します
print "value1:".$cftA->cftFunc()."<br>\n";
print "value2:".$cftB->cftFunc()."<br>\n";
print "value3:".$cftC->cftFunc()."<br>\n";
?>
<?php class SampleTest { public function cftFunc() { //static 変数名 static $x = 5; $x++; return $x+2; } } //クラスのオブジェクト作成 $cftA = new SampleTest(); $cftB = new SampleTest(); $cftC= new SampleTest(); //クラスの関数を呼び出します print "value1:".$cftA->cftFunc()."<br>\n"; print "value2:".$cftB->cftFunc()."<br>\n"; print "value3:".$cftC->cftFunc()."<br>\n"; ?>
実行結果
value1:8
value2:9
value3:10