[英]Inheriting constructors php 5.2.6
I have a relatively simple question. 我有一个相对简单的问题。 I am trying to inherit a constructor from a php superclass to authenticate on this controller.
我正在尝试从php超类继承构造函数以在此控制器上进行身份验证。
Here is my super class: 这是我的超级班:
class Auth_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
if(!session_id()){
session_start();
}
$this->load->view('login_v/logincheck');
}
}
and here is my subclass: 这是我的子类:
class Event_Controller extends Auth_Controller {
function __construct()
{
parent::__construct();
}
public function get_events_by_owner() {
$this->load->model('Event_model');
$data['events'] = $this->Event_model->select_by_owner($_SESSION['SignedIn']);
$this->load->view('event_view', $data);
}
}
This is not working. 这是行不通的。 only a white page is rendered.
仅呈现白页。 I'm not sure why it isn't working.
我不确定为什么它不起作用。 If I move the constructor from
Auth_Controller
to Event_Model
this works. 如果我将构造函数从
Auth_Controller
移到Event_Model
, Auth_Controller
可以Event_Model
工作。
Thanks! 谢谢!
EDIT: 编辑:
Fatal error: Class 'Auth_Controller' not found in
../controllers/event_controller.php on line 12
Your solution is not going to work, you should try either: 您的解决方案将无法正常工作,您应该尝试以下任一方法:
Move this code: 移动此代码:
if(!session_id()){
session_start();
}
$this->load->view('login_v/logincheck');
to a cutom library, than run this library within constructor of your controller. 到一个cutom库,而不是在控制器的构造函数中运行该库。 Please read Creating Libraries for details.
有关详细信息,请阅读创建库 。
or: 要么:
Create MY_Controller class and put auth code (quoted above) into its constructor. 创建MY_Controller类,并将身份验证代码(上面引用)放入其构造函数中。 Than you'll be able to extend like:
比您将能够扩展如下:
class Event_Controller extends MY_Controller {
(...)
Please read Creating Core System Classes for details. 有关详细信息,请阅读创建核心系统类 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.