Codeigniter辅助程序正在扩展


Codeigniter helper extending

控制器/test.php

<?php
class Test extends Controller {
    function __construct() {
    }
    function show_date(){
        $this->load->helper('date');
        echo "current date in mysql format" . date_mysql();
    }
}
?>

应用程序/助手

<?php
function date_mysql(){
    if(!time){
        $time = time();
     }
     return date('Y-m-d H-i-s', $time);
}
?>

并得到错误:

致命错误:对中的非对象调用成员函数helper()F: 第12行上的''Xampp''htdocs''ci_series''application''controllers''test.php

我能做什么??

您需要将父函数添加到__constructor函数中。像这样;

function __construct()
{
    parent::__construct();
}

这个问题应该对你有所帮助;

PHP Codeigniter-父级::__construct

使用CI_Controller

class Test extends CI_Controller {

我刚刚在CI 2.x和CI 3 上进行了测试

应用程序/控制器/Test.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
    function show_date() {
        $this->load->helper('date');
        echo "current date in mysql format " . date_mysql();
    }
}
?>

application/helpers/date_helper.php

<?php
function date_mysql( $time = false ){
    return date('Y-m-d H-i-s', !$time ? time() : $time);
}
?>

什么是有用的?