Allow "static" type
| From: | Marc Bennewitz | Date: | Fri, 27 Jan 2017 21:53:56 +0000 |
| Subject: | Allow "static" type | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-97998@lists.php.net to get a copy of this message | ||
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