4

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";

        }

2 Answers 2

3

$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.

Sign up to request clarification or add additional context in comments.

2 Comments

tried the suggested solution, nothing's changed, still the same result.
This wasn't the question, if you'd scroll up. I've added some debug output to your code.
1

You can replace it with get_class()

It seems that some versions of php don't support $this::class. It was implemented in php version 5.5. but I am using php version 7.4.12 and it throws the error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.