【问题标题】:Codeigniter helper extendingCodeigniter 助手扩展
【发布时间】:2014-11-11 14:35:14
【问题描述】:

控制器/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:\Xampp\htdocs\ci_series\application\controllers\test.php 在第 12 行

我能做什么??

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

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

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

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

    PHP Codeigniter - parent::__construct

    【讨论】:

    • 谢谢兄弟,但你能告诉我,为什么这里需要这个??
    • 道歉。我已经更新了我的答案,提供了一个可以帮助你的 URL。
    【解决方案2】:

    使用 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);
    
    }
    ?>
    

    什么有用?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多