Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jlrdw's avatar
Level 75

PHP general question - 2

This works perfect:

    public static function __callStatic($method, $params)
    {
        $instance = SomeModel::class;
        $c = new $instance;
        return $c->$method(...array_values($params));
    

But I notice the new keyword does not work with the special ::class constant.

$instance = new SomeModel::class;

Does not work.

So question is, is this a completely wrong use case for the special ::class constant?

Should I just use:

$instance = new SomeModel();

In the above use case, or is the way I used correct in the working example? Thanks.

0 likes
4 replies
bobbybouwmann's avatar
Level 88

Yeah ::class will return the full path of the class as a string. So it's not giving back the reference of the class

The special ::class constant is available as of PHP 5.5.0, and allows for fully qualified class name resolution at compile time, this is useful for namespaced classes:

You can create a class using the following actions

$instance = new SomeModel();
$instance = new SomeModel;

$class = SomeModel::class;
$instance = new $class();
jlrdw's avatar
Level 75

@BOBBYBOUWMANN - So basically I am doing

$class = SomeModel::class;
$instance = new $class();

So I guess you are saying that:

 public static function __callStatic($method, $params)
    {
        $instance = SomeModel::class;
        $c = new $instance;
        return $c->$method(...array_values($params));
    }

Is doing it correct? Thanks for reply.

But no matter, in the callstatic I have, you will always need the new keyword, is that correct.

jlrdw's avatar
Level 75

I primarily use laravel now, but an old custom framework I keep updated, even php 7.3 ready now. Just modernizing some.

What's crazy, I can read this stuff in the php manual, ---nothing.

But when I study laravels api and vendor folder, and see how Taylor used the splat operator, it makes more sense. Meaning like:

return $c->$method(...array_values($params));

The three dots is the splat operator for others reading who may be new to it, also cronix helped me with the splat in another post.

Please or to participate in this conversation.