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

时间:2016-04-17 22:26:42

标签: php

可以填充这样的变量:

我有一个像这样的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 
}

对于帮助,非常感谢

1 个答案:

答案 0 :(得分:1)

它应该可以正常显示here

<?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()方法,因为属性及其vallue是在前者中设置的。

相关问题