是否可以在php中声明从函数分配的全局变量


Is it possible to declare a global variable in php that assigned from a function

是否可以填充这样的变量:

我有一个关于php的类,如下:

class Cek_list_wire_rod extends Members_Controller {
    private $table_generate;
public function ajax_list() {
    $record_num = $this->uri->segment($this->uri->total_segments());
    $list = $this->cek_list->get_datatables($record_num);
    $this->table_generate = $list;   // Create an array object
/*.....*/
}
public function generate_pdf_laporan() {
    echo "<pre>";
    print_r ($this->table_generate); //Empty 
}

感谢的帮助

它应该可以正常工作,如下所示。

<?php class Cek_list_wire_rod {
    private $table_generate;
public function ajax_list() {
    //$record_num = $this->uri->segment($this->uri->total_segments());
    //$list = $this->cek_list->get_datatables($record_num);// not needed in demo
    $this->table_generate = '555';   // populating the test data
/*.....*/
}
public function generate_pdf_laporan() {
    echo "<pre>";
    print_r ($this->table_generate); //Empty 
}
}
$test = new Cek_list_wire_rod();
$test->ajax_list(); // calling this method since the value is being populated in this method.
$test->generate_pdf_laporan();

此外,这只是使用class属性来存储数据,而不是使用全局变量。

作为代码的一部分,您需要首先调用ajax_list()方法,然后调用generate_pdf_laporan()方法,因为属性及其值是在前者中设置的。