Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.64 KB | None | 0 0
  1. public function test(AAA(BBB) ?$value)                                              {}
  2.  
  3. AAA type hints an interface, BBB is the predefined autobox class which must implement AAA
  4.  
  5. // -----------------------------------------------------------------------------------------
  6.  
  7. interface iMyString{}
  8. class MyString implements iMyString{}
  9. class MyUTF8String implements iMyString{}
  10.  
  11. class BinThingy {
  12.     public function sendData (iMyString(MyString) ?$value)                          {}
  13. }
  14.  
  15. class UTF8Thingy extends BinThingy {
  16.     /** @override */
  17.     public function sendData (iMyString(MyUTF8String) ?$value)                      {}
  18.     // ^ works, signature is the same, only default autobox class changes
  19. }
  20.  
  21. (new BinThingy)->sendData("foo");  // creates a MyString      w/o calling the ctor and fills it via MyString->__autobox("foo")
  22. (new UTF8Thingy)->sendData("foo"); // creates a MyUTF8String  w/o calling the ctor and fills it via MyUTF8String->__autobox("foo")
  23.  
  24. // all valid:
  25. (new BinThingy)->sendData(new MyString("foo"));
  26. (new BinThingy)->sendData(new MyUTF8String("foo"));
  27. (new UTF8Thingy)->sendData(new MyString("foo"));
  28. (new UTF8Thingy)->sendData(new MyUTF8String("foo"));
  29.  
  30. // -----------------------------------------------------------------------------------------
  31.  
  32. class MyString implements iMyString
  33. {
  34.     function __autobox($value)
  35.     {
  36.          if(is_string($value))
  37.                  $this->storage = $value;
  38.          else ....
  39.     }
  40. }
  41.  
  42. class MyUTF8String implements iMyString
  43. {
  44.     function __autobox($value)
  45.     {
  46.          if(is_string($value))
  47.                  $this->storage = $value;
  48.          else ....
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement