Php 从$this获取变量->;变量
可能重复:Php 从$this获取变量->;变量,php,Php,可能重复: 我有一个类,其中一个方法返回调用它的实例。如何直接从函数的返回值访问属性(其名称存储在变量中)?以下是我正在尝试的: class MyClass { public $variable_one; public function function_one() { $variable = 'last'; // The problematic line: call method, access property on result
我有一个类,其中一个方法返回调用它的实例。如何直接从函数的返回值访问属性(其名称存储在变量中)?以下是我正在尝试的:
class MyClass {
public $variable_one;
public function function_one() {
$variable = 'last';
// The problematic line: call method, access property on result
return $this->function_two->$variable;
}
public function function_two($params = array()) {
if (is_array($params)) {
$params = http_build_query($params, NULL, '&');
}
$this->option(CURLOPT_COOKIE, $params);
return $this;
}
}
$this
变量是特殊的,只存在于类中。如果在名为variable的类中有一个属性,则可以从该类中使用$this->variable
访问该属性
class MyClass
{
private $variable;
public function getVariable()
{
return $this->variable;
}
}
你什么意思
$this->一些函数->$variable
?嗨@bfavareto我不能编辑我的问题。请看这个,我想你正在寻找方法链接。请参阅上面的链接。您的脚本包含一些错误,$变量不是仅在类中使用方法设置的,您需要定义变量。但若你们想调用函数,那个么返回局部变量。那你就不能用链锁了。只有在类范围内定义变量时,才能使用链接。函数2也缺少括号。我无法编辑我的问题。请看这个