Php 从类外返回变量

Php 从类外返回变量,php,zend-framework,variables,return,Php,Zend Framework,Variables,Return,我知道这可能是迄今为止我问过的最愚蠢的问题。。。但我一直在寻找参考点,有太多的随机问题,这是最难找到的。 我知道这很可能是正确语法的问题,但就我的一生而言,经过多次尝试和错误,我知道这可能只是更容易问。。。。(另外,lastInsertId()将不是一个可能的解决方案,因为还有其他省略的代码)。。。是否必须是全局变量才能正确返回。。。据我所知。。。。不明确 问题:如何正确回显此变量: 回音码 类代码(不同的*.php) 如果我理解正确的话。。函数正在返回所需的值。因此,将函数调用分配给变量: $

我知道这可能是迄今为止我问过的最愚蠢的问题。。。但我一直在寻找参考点,有太多的随机问题,这是最难找到的。 我知道这很可能是正确语法的问题,但就我的一生而言,经过多次尝试和错误,我知道这可能只是更容易问。。。。(另外,lastInsertId()将不是一个可能的解决方案,因为还有其他省略的代码)。。。是否必须是全局变量才能正确返回。。。据我所知。。。。不明确

问题:如何正确回显此变量:

回音码 类代码(不同的*.php)
如果我理解正确的话。。函数正在返回所需的值。因此,将函数调用分配给变量:


$last\u insert\u id=$customer->add\u函数($var1、$var2、$var3)

如果您需要传递的不仅仅是一个函数结果,请考虑采用建议的替代方法

class SoClassy {

protected $itemId;

public function add_function($var1,$var2,$var3)
     {
        $data = array(
            'position_one' => $var1,
            'position_two' => $var2,
            'position_three' => $var3
        );
        try {
            $this->db->insert($this->config->database->tables->table_name, $data);
            $this->itemId = $this->db->lastInsertId();
            }
        catch ...{}
        catch ...{}
    }

}


public function doSomethingElse() {  ... }

public function doItAll($var1,$var2,$var3) {
    $this->add_function($var1,$var2,$var3)
    $this->doSomethingElse();
    return $this->itemId;
}

}
如果您需要在方法中执行大量操作,您可能需要使用类似的方法,以避免它成为一个怪物


您可以在doSomethingElse和所有其他类方法中执行任何您想要的itemId(或其他类变量)操作。基本上,您将需要的所有临时变量保留在一个位置,而不使它们成为全局变量,从而冒名称冲突的风险。

但不幸的是,我不能专门使用它,因为省略了代码,它会在摘录下面运行多个插入。好吧,您的函数进行查询并获取
lastInsertID()
并将其放入
$return\u该变量中
,然后返回该值。。因此,假设您的查询内容是正确的,这就是您在
$last\u insert\u id
中得到的结果,函数本身工作得非常完美。。。。如果我使用修改后的
$last\u insert\u id=…
那么我可以使用它来设置变量?我需要的最后一点信息…如果我使用它来实际设置变量,那么它实际上会运行函数两次,这样我就可以去掉它正上方的
$customer->function…
行,正确吗?所以不要使用
$customer->add_函数($var1、$var2、$var3)$last_insert_id=$customer->$return_此_变量 function add_function($var1,$var2,$var3)
     {
        $data = array(
            'position_one' => $var1,
            'position_two' => $var2,
            'position_three' => $var3
        );
        try {
            $this->db->insert($this->config->database->tables->table_name, $data);
            $return_this_variable = $this->db->lastInsertId();
       return $return_this_variable;
            }
        catch ...{}
        catch ...{}
    }
class SoClassy {

protected $itemId;

public function add_function($var1,$var2,$var3)
     {
        $data = array(
            'position_one' => $var1,
            'position_two' => $var2,
            'position_three' => $var3
        );
        try {
            $this->db->insert($this->config->database->tables->table_name, $data);
            $this->itemId = $this->db->lastInsertId();
            }
        catch ...{}
        catch ...{}
    }

}


public function doSomethingElse() {  ... }

public function doItAll($var1,$var2,$var3) {
    $this->add_function($var1,$var2,$var3)
    $this->doSomethingElse();
    return $this->itemId;
}

}