有人能告诉我如何使用另一个函数中的变量吗?在ZEND
private $name
public function names(){
$this -> name = 'John';
}
public function leads(){
$this -> names();
echo $this -> name;
}
答案 0 :(得分:1)
选项1:通过参数传递:
public function somewhereWhereYouCallThoseFunctions() {
$namePassedToFunction = names();
leads($namePassedToFunction);
}
public function names(){
return 'John';
}
public function leads($namePassedToFunction){
echo $namePassedToFunction;
}
选项2(不更改代码):
调用函数names()
,然后调用leads()
:
private class someClass() {
private $name;
public function names(){
$this->name = 'John';
}
public function leads(){
echo $this -> name;
}
}
$someClass = new someClass();
$someClass->names();
$someClass->leads();