How to make a return type mixed and self?

class Foo 
{
    final public function get(string $path): mixed
    {
        // return mixed or self type;
    }

    final public function otherMethod()
    {

    }
}

In this example, the return value can be of any type, incl. object of oneself (static).

How to ensure that the editor PhpStorm can provide hints as:

$obj->get('path.to.any.data')->otherMethod();

Currently, due to the type, the mixed editor cannot provide a hint.

  • Yes, the mixed type causes a problem here. You could try mixed | Foo, but that will generate an error: “Fatal error: Type mixed can only be used as a standalone type”, which makes perfectly sense. I don’t think there’s a solution for this, but I don’t use PHPStorm…. so… anybody?

    – 




  • Well .. if it’s mixed (which means “any” — an int, string or even null) .. then how do you expect get('path.to.any.data')-> to actually work (as it will generate PHP runtime error on non object)? P.S. It sounds to me like you have provided an overly simplified code. How it the actual get() method works? Does it return different type base don the parameter value etc?

    – 

Leave a Comment