typehint – PHP Blog http://blog.dev-php.site Snippets and guides Fri, 12 Nov 2021 23:16:08 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.3 PHP Self Type Hint http://blog.dev-php.site/php-self-type-hint/ Sun, 01 Dec 2019 21:30:21 +0000 http://blog.dev-php.site/?p=172 Continue reading 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();


]]>
Closure vs Callable Type Hinting http://blog.dev-php.site/closure-vs-callable-type-hinting/ Thu, 28 Nov 2019 09:16:26 +0000 http://blog.dev-php.site/?p=169 Continue reading Closure vs Callable Type Hinting]]>
  • function foo(callable $bar) PHP 5.4+
  • function foo(callable $bar = null) PHP 5.4+
  • function foo(): callable PHP 7.0+
  • function foo(): ?callable PHP 7.1+
  • When type hinting, a closure must be an anonymous function, where a callable also can be a normal method/function.

    • Closure is a class and callable is a type.
    • The callable type accepts anything that can be called
    
    function callableFunc(Callable $callback) {
        $callback();
    }
    
    function closureFunc(Closure $closure) {
        $closure();
    }
    
    function anyFunc(Callable $callable) {
        $callable();
    }
    
    function testFunc() {
        echo 'Hello, World!', "
    "; } $testClosure = function(){ echo 'Hello, World!', "
    "; }; // gettype($testClosure); object // var_dump($testClosure); object(Closure)#1 (0) { } try{ closureFunc($testClosure); // Hello, World! callableFunc("testFunc"); // Hello, World! anyFunc($testClosure); // Hello, World! callableFunc($testClosure); // Hello, World! closureFunc("testFunc");// Argument 1 passed to closureFunc() must be an instance of Closure, string given } catch(TypeError $e){ echo $e->getMessage(), "
    "; } finally { echo "Reachable statement"; }
    ]]>