dongtang5229 2016-08-05 03:02
浏览 341
已采纳

Parent的类构造函数调用错误?

I'm trying to call parent class constructor but it throws an error

Fatal error: Cannot call constructor

and the same code was working well before, I didn't change anything and but suddenly don't know what could have happened, It is throwing this error.

I've read some answers on stackoverflow, but they say that your parent class doesn't contain a constructor, well, this is not my case I have constructor in my parent class. Here's my code:

class DB
{
    var $con;

    public function __construct()
    {
        require_once 'configs/dbconfig.php';
        $this->connect();
    }

    function connect()
    {
        try{
            $this->con = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME,DB_USER,DB_PASS);
            $this->con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        }catch(PDOException $ex) {
            echo $ex->getMessage();
        }
    }
}

and then I have a class Posts which is extending DB and calling DB's constructor.

class Posts extends DB
{
    var $user_id;

    public function __construct($user_id)
    {
        parent::__construct();
        $this->user_id = $user_id;
    }

    function get_posts($search,$pages,$since_id=0,$max_id=false,$count=20,$status='active')
    {
        $extra = '';
        if($search) $extra .= " AND text LIKE CONCAT('%',:search,'%')";
        if(!empty($pages)) $extra .= " AND page IN('".implode("','", $pages)."')";
        if(!empty($status) && $status != 'all') $extra .= " AND status=:status";

        $max_id = ($max_id) ? $max_id : time();

        $sqlCommand = "SELECT id,pid,text,media,media_url,type,name,u_id,username,user_profile_url,user_photo_url,post_url,date,status,source,page_id FROM ".POSTS." WHERE date>=:since_id AND date<=:max_id".$extra." AND user_id=:user_id ORDER BY date DESC LIMIT $count";
        $params = array(':since_id'=>$since_id,':max_id'=>$max_id,':user_id'=>$this->user_id);
        if($search) $params[':search'] = $search;
        if($status && $status != 'all') $params[':status'] = $status;

        $posts = $this->fetch($sqlCommand,$params);
        return $posts;
    }
}
  • 写回答

3条回答 默认 最新

  • dongshenchi5364 2016-08-20 16:02
    关注

    This is with your php version, It must have been changed, please check the version of php and try to update that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测