在另一个方法php中使用变量

时间:2014-05-21 16:13:18

标签: php function zend-framework

有人能告诉我如何使用另一个函数中的变量吗?在ZEND

private $name

public function names(){
           $this -> name = 'John';
    }

    public function leads(){
           $this -> names();
           echo $this -> name;
    }

1 个答案:

答案 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();