Hi all,

I would like to know your opinion about using "static" as type-hint similar to "self" and how simple / complex it would be to implement.


Example - using static:
https://3v4l.org/XqDma

class Base {
    public static function test(static $obj) : static {
        echo get_class($obj) . "\n";
        return new static($obj);
    }
}

class Ext extends Base {}

$base = new Base;
$ext  = new Ext;

Base::test($base);
Base::test($ext);

Ext::test($ext);
Ext::test($base);



Example - using self:
https://3v4l.org/Tp7k6

- Boilerplate code to check if the given argument is an instance of static
- No way for IDEs to test Ext::test returning an instance of Ext

class Base {
    public static function test(self $obj) : self {
        echo get_class($obj) . "\n";

        if (!$obj instanceof static) {
            throw new InvalidArgumentException(sprintf(
                "Object (%s) must be an instance of %s",
                get_class($obj),
                static::class
            ));
        }

        return new static($obj);
    }
}

class Ext extends Base {}

$base = new Base;
$ext  = new Ext;

Base::test($base);
Base::test($ext);

Ext::test($ext);
Ext::test($base);



Thanks
Marc

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to