Magic methods

7,829 views

Published on

Originally presented at Northeast PHP 2013.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,829
On SlideShare
0
From Embeds
0
Number of Embeds
25
Actions
Shares
0
Downloads
32
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Magic methods

  1. 1. Magic Methods: Spilling the Secret By Matthew Barlocker
  2. 2. The Barlocker ● Chief Architect at Lucid Software Inc ● Started using PHP in 2005 ● Graduated with BS in CS from BYU in 2008 ● Developed software for the following industries: – Network Security – Social Gaming – Financial – Productivity
  3. 3. Magic Methods ● All object methods beginning with '__' are reserved. ● To gain the magic functionality, define the method on the class. <?php class MyClass { private $var1 = 5; public function doSomething() { echo "hello!n"; } } <?php class MyClass { private $var1 = 5; public function doSomething() { echo "hello!n"; } public function __toString() { return 'This is a MyClass'; } }
  4. 4. Magic Methods ● Stringification ● Lifecycle ● Property Overloading ● Method Overloading ● Serialization ● Cloning ● Object Invocation
  5. 5. Stringification ● __toString
  6. 6. __toString ● public string __toString() ● Called when an object is cast as a string. ● Throwing an exception inside this method will cause a fatal error. ● No default implementation.
  7. 7. __toString <?php class NoTostringExample { public $a = 1; } $obj = new NoTostringExample(); echo "Stringified " . $obj . "n"; ?> $ php notostring.php PHP Catchable fatal error: Object of class NoTostringExample could not be converted to string in notostring.php on line 8
  8. 8. __toString <?php class TostringExample { public $a = 2; public function __toString() { return 'TostringExample(' . $this->a . ')'; } } $obj = new TostringExample(); echo "Stringified " . $obj . "n"; ?> $ php tostring.php Stringified TostringExample(2)
  9. 9. __toString <?php class BadTostringExample { public $a = 3; public function __toString() { echo 'TostringExample(' . $this->a . ')' . "n"; } } $obj = new BadTostringExample(); echo "Stringified " . $obj . "n"; ?> $ php badtostring.php TostringExample(3) PHP Catchable fatal error: Method BadTostringExample::__toString() must return a string value in badtostring.php on line 11
  10. 10. Lifecycle ● __construct ● __destruct
  11. 11. __construct ● public void __construct($params, …) ● Called when an object is first initialized. ● Must explicitly call parent::__construct() in children. ● Default implementation does nothing.
  12. 12. __destruct ● public void __destruct() ● Called when an object is garbage collected. ● Must explicitly call parent::__destruct() in children. ● Default implementation does nothing.
  13. 13. __destruct ● public void __destruct() ● Called when an object is garbage collected. ● Must explicitly call parent::__destruct() in children. ● Default implementation does nothing.
  14. 14. __construct / __destruct $ php construct.php Starting Construct Ending Destruct <?php class Construct { public function __construct() { echo "Constructn"; } public function __destruct() { echo "Destructn"; } } echo "Startingn"; $obj = new Construct(); echo "Endingn"; ?>
  15. 15. __construct / __destruct <?php class MyDB { private $connection = null; public function __construct($host, $user, $pass) { $this->connection = dbconnect($host, $user, $pass); } public function __destruct() { $this->connection->close(); } }
  16. 16. Property Overloading ● __get ● __set ● __isset ● __unset
  17. 17. __get ● public mixed __get($name) ● Called when an inaccessible property is read. ● Not called in chains. ($x = $obj->noexist = 5;) ● Default implementation emits a warning and returns null or emits a fatal error. ● Does not apply to static context.
  18. 18. __set ● public void __set($name, $value) ● Called to write a value to an inaccessible property. ● Default implementation adds a public variable to the class. ● Does not apply to static context.
  19. 19. __isset ● public mixed __isset($name) ● Triggered by calling isset() or empty() on inaccessible properties ● Default implementation checks for existence of property and ignores visibility. ● Does not apply to static context.
  20. 20. __unset ● public mixed __unset($name) ● Triggered by calling unset() on inaccessible properties ● Default implementation removes accessible properties. ● Does not apply to static context.
  21. 21. Property Test See code from http://www.php.net/manual/en/language.oop5.overloading.php#object.get
  22. 22. Method Overloading ● __call ● __callStatic
  23. 23. __call ● public mixed __call($name, $params) ● Called when invoking inaccessible methods from an object context.
  24. 24. __callStatic ● public static mixed __callStatic($name, $params) ● Called when invoking inaccessible methods from a static context.
  25. 25. __call / __callStatic <?php class MethodTest { public function __call($name, $arguments) { echo "Calling object method '$name' " . implode(', ', $arguments). "n"; } public static function __callStatic($name, $arguments) { echo "Calling static method '$name' " . implode(', ', $arguments). "n"; } } $obj = new MethodTest; $obj->runTest(1, 2, 3, 'in object context'); MethodTest::runTest(4, 5, 6, 'in static context'); // As of PHP 5.3.0 ?> $ php methodtest.php Calling object method 'runTest' 1, 2, 3, in object context Calling static method 'runTest' 4, 5, 6, in static context
  26. 26. Serialization ● __sleep ● __wakeup
  27. 27. __sleep ● public array __sleep() ● Called when serialize() is called on the object. ● Returns an array of field names to include in the serialized version of the object.
  28. 28. __wakeup ● public void __wakeup() ● Called when unserialize() is called on the serialized object.
  29. 29. __sleep / __wakeup See code from http://www.php.net/manual/en/language.oop5.magic.php#object.sleep
  30. 30. Cloning ● __clone ● __set_state
  31. 31. __clone ● public void __clone() ● Called after the object is initialized. ● Default implementation does nothing.
  32. 32. __clone See code from http://www.php.net/manual/en/language.oop5.cloning.php#object.clone
  33. 33. __set_state ● public static void __set_state($properties) ● Called for classes exported by var_export().
  34. 34. __set_state See code from http://www.php.net/manual/en/language.oop5.magic.php#object.set-state
  35. 35. Object Invocation ● __invoke
  36. 36. __invoke ● public mixed __invoke($params, ...) ● Called when the object is treated like a function. – $obj(1,2,3);
  37. 37. __invoke <?php class CallableClass { public function __invoke($x) { var_dump($x); } } $obj = new CallableClass; $obj(5); var_dump(is_callable($obj)); ?> $ php invoke.php int(5) bool(true)
  38. 38. Performance ● Each benchmark was run 10 times. ● Each run is shown as a different set of columns in the graphs. ● Each run is exactly 1 million calls to the item being tested.
  39. 39. Performance
  40. 40. Performance
  41. 41. Performance
  42. 42. Performance
  43. 43. Performance
  44. 44. Performance
  45. 45. Thank you for your time. Any Questions?
  46. 46. Lucid Software Inc ● Building the next generation of collaborative web applications ● VC funded, high growth, profitable ● Graduates from Harvard, MIT, Stanford ● Team has worked at Google, Amazon, Microsoft https://www.lucidchart.com/jobs

×