Error calling parent class constructor?

I try to call the constructor of the parent class, but it throws an error

Fatal error: Cannot call constructor

and the same code worked well before, I didnโ€™t change anything and, but suddenly I donโ€™t know what could happen, it throws this error.

I read a few answers on stackoverflow, but they say that your parent class does not contain a constructor, well, this is not my case. I have a constructor in my parent class. Here is 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 Posts class that extends DB and calls the DB 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;
    }
}
+4
source share
3 answers

This is with your php version. Perhaps it has been changed, check the php version and try updating it.

+1
source

, PHP ( ), DB, DB ( ) Posts .

-1

Well, maybe this is the main stupid answer, but you tried to โ€œdemandโ€?

-2
source

Source: https://habr.com/ru/post/1650250/


All Articles