Посмотрите на этот пример, главное отличие состоит в том, что у вас нет доступа в другом методе к c значению, объявленному внутри функции
<?php
class StaticProperty
{
private static $staticProperty = 'init property';
private function someFunction()
{
if ('init property' === self::$staticProperty) {
//...havy work.
self::$staticProperty = 'my result in property';
}
return self::$staticProperty;
}
public function getResult()
{
var_dump(self::$staticProperty);
return $this->someFunction();
}
}
class StaticVariable
{
private function someFunction()
{
static $staticVar = 'init variable';
if ('init variable' === $staticVar) {
//...havy work.
$staticVar = 'my result in variable';
}
return $staticVar;
}
public function getResult()
{
//var_dump(self::$staticVar);// it will call "Uncaught Error: Access to undeclared static property:.."
return $this->someFunction();
}
}
$staticVariable = new StaticVariable();
var_dump($staticVariable->getResult());
$staticPropertyClass = new StaticProperty();
var_dump($staticPropertyClass->getResult());
Результат:
string(21) "my result in variable"
string(13) "init property"
string(21) "my result in property"