'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";
}
Solution 1:[1]
$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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
