更新时间:2023-11-14 23:39:40
您的问题与类的方法/属性和对象的差异有关.
Your problem is connected with differences between class's methods/properties and object's.
trait Example
{
protected static $var;
protected $var2;
private static function printSomething()
{
print self::$var;
}
private function doSomething()
{
print $this->var2;
}
}
class NormalClass
{
use Example;
public function otherFunction()
{
self::printSomething();
$this->doSomething();
}
public function setVar($string, $string2)
{
self::$var = $string;
$this->var2 = $string2;
}
}
$obj = new NormalClass();
$obj -> setVar('first', 'second');
$obj -> otherFunction();
静态函数printSomething无法访问非静态属性$ var! 您应该将它们定义为非静态或静态.
Static function printSomething can't access not static propertie $var! You should define them both not static, or both static.