我是 PHP OOP 的新手,如何在类内的函数之间共享或传递变量?
class Sample {
public function One() {
$var1 = 'abc';
$var2 = 'xyz';
return $var1;
}
public function Two() {
$var3 = $var1.$var2;
return $var3;
}
}
还是可以返回多个值?
谢谢。
更新
class Sample {
// This is how you declare class members
public $var1, $var2;
public function One() {
// You use $this to refer class memebers
$this->var1 = 'abc';
$this->var2 = 'xyz';
return $this->var1;
}
public function Two() {
$var3 = $this->var1.$this->var2;
return $var3;
}
}
$test = new Sample();
echo $test->Two();
我已经测试了一个提供的示例,当调用函数 Two() 时它在我的页面中返回空白,知道吗?