Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function test(AAA(BBB) ?$value) {}
- AAA type hints an interface, BBB is the predefined autobox class which must implement AAA
- // -----------------------------------------------------------------------------------------
- interface iMyString{}
- class MyString implements iMyString{}
- class MyUTF8String implements iMyString{}
- class BinThingy {
- public function sendData (iMyString(MyString) ?$value) {}
- }
- class UTF8Thingy extends BinThingy {
- /** @override */
- public function sendData (iMyString(MyUTF8String) ?$value) {}
- // ^ works, signature is the same, only default autobox class changes
- }
- (new BinThingy)->sendData("foo"); // creates a MyString w/o calling the ctor and fills it via MyString->__autobox("foo")
- (new UTF8Thingy)->sendData("foo"); // creates a MyUTF8String w/o calling the ctor and fills it via MyUTF8String->__autobox("foo")
- // all valid:
- (new BinThingy)->sendData(new MyString("foo"));
- (new BinThingy)->sendData(new MyUTF8String("foo"));
- (new UTF8Thingy)->sendData(new MyString("foo"));
- (new UTF8Thingy)->sendData(new MyUTF8String("foo"));
- // -----------------------------------------------------------------------------------------
- class MyString implements iMyString
- {
- function __autobox($value)
- {
- if(is_string($value))
- $this->storage = $value;
- else ....
- }
- }
- class MyUTF8String implements iMyString
- {
- function __autobox($value)
- {
- if(is_string($value))
- $this->storage = $value;
- else ....
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement