在两个函数PHP中使用相同的数组

时间:2015-06-30 11:55:13

标签: php arrays

我想知道如何在第二个函数中取回我的数组:CreatejsonFile?我尝试使用global和GLOBALS,但它不起作用。

class Connection { //my class

public static function connect($request) { // function call database

$connect = oci_connect($user, $password, $connection_url3,'AL32UTF8'); 
$stmt = ociparse($connect,$request);
ociexecute($stmt,OCI_DEFAULT); 

$txt_job_aval = array();

while (ocifetch($stmt)){ 
array_push($txt_job_aval, ociresult($stmt,1)); 
}

public function CreatejsonFile(){

foreach($txt_job_aval as $valeur) 
    { 
    echo $valeur.'<br>'; 
    } 
}
}

1 个答案:

答案 0 :(得分:3)

不要把它变成全局......让它成为类的属性

class Connection { //my class

    protected $txt_job_aval;

    public function connect($request) { // function call database

        $connect = oci_connect($user, $password, $connection_url3,'AL32UTF8'); 
        $stmt = ociparse($connect,$request);
        ociexecute($stmt,OCI_DEFAULT); 

        $this->txt_job_aval = array();

        while (ocifetch($stmt)){ 
            array_push($this->txt_job_aval, ociresult($stmt,1)); 
        }
    }

    public function CreatejsonFile(){
        foreach($this->txt_job_aval as $valeur) { 
            echo $valeur.'<br>'; 
        } 
    }
}

并致电

$con = new Connection();
$con->connect($request);
$con->CreatejsonFile();

OR

class Connection { //my class

    protected static $txt_job_aval;

    public function connect($request) { // function call database

        $connect = oci_connect($user, $password, $connection_url3,'AL32UTF8'); 
        $stmt = ociparse($connect,$request);
        ociexecute($stmt,OCI_DEFAULT); 

        self::txt_job_aval = array();

        while (ocifetch($stmt)){ 
            array_push(self::txt_job_aval, ociresult($stmt,1)); 
        }
    }

    public static function CreatejsonFile(){
        foreach(self::txt_job_aval as $valeur) { 
            echo $valeur.'<br>'; 
        } 
    }
}

并致电

Connection::connect($request);
Connection::CreatejsonFile();