PHP Self Type Hint

Self Function Parameter

The self type hint refers to the current class not to the instance ie. an object of the same type (the current class or a subclass). Every variable passed must be an instanceof the same class.


function foo(self $bar)		// PHP 5.0+
function foo(self $bar = null)	// PHP 5.1+


class foo { 
	function aMethod(self $obj){
		echo var_dump($obj);	
	}
}
class bar extends foo{}
class baz extends bar{}

$e = new foo();
$f = new bar();
$g = new baz();

$e->aMethod($e); // object(foo)#1 (0) { }
$e->aMethod($f); // object(bar)#2 (0) { }
$e->aMethod($g); // object(baz)#3 (0) { }


Self Return Type

If you use self return type it doesn’t matter if the method returns a cloned version of the object, (immutable objects). The object that is returned is of the same type as the object on which the method is called. In the example below whatever is returned will be of type Foo.

function foo(): self	// PHP 7.0+
function foo(): ?self	// PHP 7.1+

interface Foo {
    /**
     * @param Bar $bar
     * @return self
     */
    public function setBar(Bar $bar): self;
}

class Foo implements Foo
{
    /**
     * {@inheritdoc} 
     */
    public function setBar(Bar $bar): Foo
    {
        // ...
        return $this;
    }
}


class ClassName
{
    public function foo(): ?self
    {
        return new ClassName();
    }

    public function bar(): ?self
    {
        return null;
    }
}

$instance = new ClassName();
$instance->foo();
$instance->bar();