Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Fatal error: Cannot use ::class with dynamic class name

I uploaded a website to a live server with this code

class Model extends Database
{
    public $errors = array();

    public function __construct()
    {
        // code...
        if(!property_exists($this, 'table'))
        {
            $this->table = strtolower($this::class) . "s";
            
        }
    }
}

but I keep getting this error: PHP fatal error: cannot use ::class with dynamic class name I've tried using the get_class() function but I don't think I'm using it properly, cause it takes me the controller not found page in the else block

    public function __construct()
    {
        // code...
        $URL = $this->getURL();
        if(file_exists("../private/controllers/".$URL[0].".php"))
        {
            $this->controller = ucfirst($URL[0]);
            unset($URL[0]);
        }else
        {
            echo"<center><h1>controller not found: " . $URL[0] . "</h1></center>";
            die;
        }

here's how I've been using it

if  (!property_exists($this, 'table'))
        {
            $this->table = strtolower(get_class($this)) . "s";

        }
like image 528
Ahbiggie Avatar asked Sep 13 '25 08:09

Ahbiggie


1 Answers

$this::class is non-sense, because you cannot use the scope resolution operator on an object's instance. Instead use the default predefined constant __CLASS__ for the current class. Or function get_class(object $object = ?): string to get the class from any object's instance.

like image 184
Martin Zeitler Avatar answered Sep 14 '25 21:09

Martin Zeitler