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