Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 面向对象的公共变量问题_Php_Oop - Fatal编程技术网

Php 面向对象的公共变量问题

Php 面向对象的公共变量问题,php,oop,Php,Oop,有人能帮忙吗?因为某些原因,我可以在导出函数中回显和设置文件和部门变量,但当我尝试在exportdata中调用它们时,它们是空的 Class customersController Extends baseController { public $file; public $department; public function export() { $this->registry->template->title = 'Ainswo

有人能帮忙吗?因为某些原因,我可以在导出函数中回显和设置文件和部门变量,但当我尝试在exportdata中调用它们时,它们是空的

Class customersController Extends baseController {
    public $file;
    public $department;


    public function export() {
        $this->registry->template->title = 'Ainsworthstudio - Export Customer Info';

        $this->registry->model->createModel('db', 'db');
        $this->registry->model->createModel('customers', 'customers');
        $this->registry->model->getModel('db')->addConnection("localhost", "sdgsdg", "sdg0", "sdg");
        $data = $this->registry->model->getModel('customers')->getDepartments();

        while(list($k, $v)=each($data))
            $$k = $v;

        $this->registry->template->departments = $data;
        $this->registry->template->show('customers_export');
    }

    public function exportcheck() {
        if(!empty($_POST['filename'])) {
            $this->file =  $_POST['filename'];
            $this->department =  $_POST['depart'];

            echo "<div class='error_message'>Exported Successfully</div>";

            echo $this->file;
            echo $this->department;
            echo 'success';
        } else {
            echo "<div class='error_message'>Wrong Username or Password</div>";
            exit();
        }
    }

    public function exportdata() {
        return $this->file;
        return $this->department;

        echo $this->department;
    }
}
类CustomerController扩展baseController{
公共文件;
公共财政部;
公共功能导出(){
$this->registry->template->title='Ainsworthstudio-导出客户信息';
$this->registry->model->createModel('db','db');
$this->registry->model->createModel('customers','customers');
$this->registry->model->getModel('db')->addConnection(“localhost”、“sdgsdg”、“sdg0”、“sdg”);
$data=$this->registry->model->getModel('customers')->getDepartments();
while(列表($k,$v)=每个($data))
$$k=$v;
$this->registry->template->departments=$data;
$this->registry->template->show('customers_export');
}
公共函数导出检查(){
如果(!空($\u POST['filename'])){
$this->file=$\u POST['filename'];
$this->department=$_POST['department'];
echo“导出成功”;
echo$this->file;
echo$this->department;
呼应"成功",;
}否则{
回显“错误的用户名或密码”;
退出();
}
}
公共函数exportdata(){
返回$this->file;
返回$this->department;
echo$this->department;
}
}
您的问题在于:

public function exportdata() {
    return $this->file;
    return $this->department;

    echo $this->department;
}
您正在返回
$this->file
的值,但该值没有响应

如果尝试
echo$myCustomersController->exportdata()
您将看到它回显
$this->file的值

如果要
回显
值,请删除
返回
s

public function exportdata() {
    echo $this->file;
    echo $this->department;
}

如果您可以缩进代码并删除不必要的代码,这样愿意帮助您的人就可以更轻松地阅读它,那将是非常好的。其次,您是否对这些属性设置过任何设置?是的,请清除代码并演示如何使用它。这是一个CI框架,对吗?对不起,这是一个我正在构建的框架,我仍然需要将很多东西简化为函数,我只是想让一切正常工作。似乎所有内容都按要求分配,但回音永远不会运行!我用echo试了一下,什么也没发生。我使用的是MVC,所以他们页面上的每个函数都是不同的,这会使它无法工作>@Josh,是的,会的。本质上,当您查看与
exportdata()
对应的页面时,不会调用
exportcheck()
的代码,因此不会设置
$this->file
$this->department
值。您需要做的是从设置
$this->file
$this->department
值的
exportcheck()
中获取代码,并将其分离为一个可以从
exportcheck()和
exportdata()调用的函数
.im使用ajax实际表单post正在调用exportcheck,如果成功转到exportdataSo当用户单击submit时,您正在发布到exportcheck如果成功,浏览器将重定向到exportdata?如果是这样,则不要将department和file设置为$this,而是将它们设置为会话或cookie。这样,您就可以在请求之间持久化它们