我需要在方法onec中分配a中的b值,但它失败了。请告诉我我在这里做错了什么:
<?php
class One {
public $a = 10;
public $b = 20;
public static function onec() {
$this->a = $this->b;
return $this->a;
}
}
echo One::onec();
?>发布于 2014-05-05 09:41:30
使用self关键字。在静态上下文中无法访问$this关键字。另外,您应该将变量设置为static
像这样..。
<?php
class One {
public static $a = 10;
public static $b = 20;
public static function onec() {
self::$a = self::$b;
return self::$a;
}
}
echo One::onec();发布于 2014-05-05 09:44:20
在静态函数中使用$this。http://www.php.net/manual/en/language.oop5.static.php
<?php
class One {
public $a = 10;
public $b = 20;
public static function onec() {
$obj = new One();
$obj->a = $obj->b;
return $obj->a;
}
}
echo One::onec();发布于 2014-05-05 09:54:58
使用此代码
class One {
public $a = 10;
public $b = 20;
public function onec() {
$this->a = $this->b;
return $this->a;
}
}
$obj = new One();
echo $obj->onec();https://stackoverflow.com/questions/23469306
复制相似问题