這個問題在這里已經有了答案: PHP 中的靜態變數 (5 個回答) 我什么時候在 php 中使用靜態變數/函式? (8 個回答) 11 小時前關閉。
我想知道下面這樣的靜態陣列有什么區別:
static $lang = array(
'message' => 'Welcome ',
'admin' => 'administrator'
);
和這個陣列:
$lang = array(
'message' => 'Welcome ',
'admin' => 'administrator'
);
uj5u.com熱心網友回復:
如果屬性是靜態的,則無需創建此類的實體即可訪問它。
//static
class MyClass(){
static $lang = array(
'message' => 'Welcome ',
'admin' => 'administrator'
);
}
var_dump (MyClass::$lang)
//public
class MyClass(){
public $lang = array(
'message' => 'Welcome ',
'admin' => 'administrator'
);
}
$obj = new MyClass();
var_dump($obj->lang);
靜態屬性使用范圍決議運算子 ( ::) 訪問,不能通過物件運算子 ( ->)訪問。
靜態變數只存在于區域函式作用域中,但在程式執行離開該作用域時不會失去其值。
function myFunction()
{
$nonStatic = 0;
echo $nonStatic;
$nonStatic ;
}
myFunction(); // OUTPUT: 0
myFunction(); // OUTPUT: 0
myFunction(); // OUTPUT: 0
myFunction(); // OUTPUT: 0
當$nonStatic變數失去其值時,函式 test 總是回傳零。
在此功能中:
function myFunction()
{
static $staticVar = 0;
echo $staticVar;
$staticVar ;
}
myFunction(); // OUTPUT: 0
myFunction(); // OUTPUT: 1
myFunction(); // OUTPUT: 2
myFunction(); // OUTPUT: 3
因為$staticVar變數是靜態的,所以它保留了它的值。
這同樣適用于您的陣列
function test()
{
static $lang = array(
'message' => 'Welcome ',
'admin' => 'administrator'
);
var_dump($lang);
$lang['message'] = 'new message ';
}
test(); // OUTPUT: array(2) { ["message"]=> string(6) "Welcome" ["admin"]=> string(9) "administrator" }
test(); // OUTPUT: array(2) { ["message"]=> string(6) "new message" ["admin"]=> string(9) "administrator" }
要從類內部訪問靜態變數或方法,您可以使用self關鍵字。要訪問靜態變數或從包含靜態變數的類擴展的類中呼叫方法,您可以使用parent關鍵字
class MyClass{
static $lang = array(
'message' => 'Welcome ',
'admin' => 'administrator'
);
static function myFunction(){
return self::$lang;
}
}
class ScndClass extends MyClass{
public function callStaticFunction(){
var_dump(parent::$lang);
return parent::$myFunction();
}
}
$obj = new ScndClass();
echo $obj->callStaticFunction();
閱讀有關關鍵字的更多資訊static。
uj5u.com熱心網友回復:
請查看此 https://www.php.net/manual/en/language.variables.scope.php#language.variables.scope.static https://www.php.net/manual/en/language.oop5。 static.php#language.oop5.static.methods https://www.php.net/manual/en/language.oop5.static.php#language.oop5.static.properties
靜態變數只存在于區域函式作用域中,但在程式執行離開該作用域時不會失去其值。
所以在下面的例子中:
function test()
{
$a = 0;
echo $a;
$a ;
}
test(); // OUTPUT: 0
test(); // OUTPUT: 0
test(); // OUTPUT: 0
test(); // OUTPUT: 0
當$a變數失去其值時,函式 test 總是回傳零。
在此功能中:
function test()
{
static $a = 0;
echo $a;
$a ;
}
test(); // OUTPUT: 0
test(); // OUTPUT: 1
test(); // OUTPUT: 2
test(); // OUTPUT: 3
因為$a變數是靜態的,所以它保留了它的值。
同樣在陣列中,
function test()
{
static $lang = array(
'message' => 'Welcome ',
'admin' => 'administrator'
);
var_dump($lang);
$lang['message'] = 'Hello ';
}
test(); // OUTPUT: array(2) { ["message"]=> string(6) "Welcome" ["admin"]=> string(9) "administrator" }
test(); // OUTPUT: array(2) { ["message"]=> string(6) "Hello" ["admin"]=> string(9) "administrator" }
在前面的例子中,第一次運行將輸出“Welcome administartor”,而第二次運行將輸出“Hello administrator”。
還,
Static variables can be assigned values which are the result of constant expressions, but dynamic expressions, such as function calls, will cause a parse error.
so the following example will cause error
$name = "Omar";
static $lang = array(
'message' => 'Welcome ',
'admin' => 'administrator' . $name,
);
var_dump($lang); // Fatal error: Constant expression contains invalid operations
While the following code is valid one:
$name = "Omar";
$lang = array(
'message' => 'Welcome ',
'admin' => 'administrator' . $name,
);
var_dump($lang); // OUTPUT: array(2) { ["message"]=> string(9) "Welcome " ["admin"]=> string(12) "administratorOmar" }
Also note this:
Static properties are accessed using the Scope Resolution Operator (::) and cannot be accessed through the object operator (->).
It's possible to reference the class using a variable. The variable's value cannot be a keyword (e.g. self, parent and static).
So the following example is valid
class Foo
{
static $lang = array(
'message' => 'Welcome ',
'admin' => 'administrator'
);
}
var_dump(Foo::$lang); // array(2) { ["message"]=> string(9) "Welcome " ["admin"]=> string(9) "administrator" }
While the following example will throw fatal error
class Foo
{
public $lang = array(
'message' => 'Welcome ',
'admin' => 'administrator'
);
}
var_dump(Foo::$lang); // Fatal error: Uncaught Error: Access to undeclared static property Foo::$lang
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/357425.html
