Successfully reported this slideshow.

PHP 5 Magic Methods



Upcoming SlideShare
Magic methods
Magic methods
Loading in …3
1 of 54
1 of 54

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

PHP 5 Magic Methods

  1. 1. PHP 5 Magic Functions<br />Front Range PHP Users Group<br /><br />February 10, 2010<br />
  2. 2. PHP 5 Magic Functions<br />All functions start with __<br />PHP reserves all function names starting with __<br />Don’t define them unless you want the magic functionality<br />
  3. 3. Magic Methods in this Presentation<br />__construct()<br />__destruct()<br />__toString()<br />__get()<br />__set()<br />__isset()<br />__unset()<br />__call()<br />__clone<br />__set_state()<br />
  4. 4. Magic Methods briefly covered<br />__callStatic()<br />__sleep()<br />__wakeup()<br />__invoke()<br />__autoload()<br />
  5. 5. New to PHP 5<br />Magic functions (in general) allow you to define class functionality without needing to duplicate code<br />I say in general since some of them don’t really seem to follow that definition and I didn’t find a definition that covers them all.<br />
  6. 6. __construct()<br />If you’ve done any OOP in PHP5, you’ve probably run into this method.<br />Used to initialize an object.<br />Best practices say to make sure you don’t do work in the constructor.<br />Just assign values<br />… and remember Dependency Injection (coming up next)<br />
  7. 7. __construct()<br />Constructor is setting values, but not doing work<br />
  8. 8. __construct()<br />Doing work in constructors makes it harder to reuse the class.<br />
  9. 9. __construct()<br />If you don’t provide a constructor method, and extend a class, the parent constructor will be called automatically.<br />If you do provide a constructor, and want the parent constructor called, you must called parent::__construct()<br />
  10. 10. __construct()<br />By making the constructor inaccessible, you can prevent external instantiation.<br />For example, implementing the singleton pattern, we don’t want external instantiation since we cannot control how many objects are created.<br />
  11. 11. __destruct()<br />__destruct() is called when <br />all references to an object are removed<br />object is explicitly destroyed<br />shutdown sequence is initiated<br />
  12. 12. __destruct()<br />Parent destructors must be called explicitly if destructor is provided<br />
  13. 13. __destruct()<br />You could use this method to ensure an object logs itself.<br />
  14. 14. __destruct()<br />The destructor is called even if a script exits due to a call to exit()<br />However, if you call exit() in a destructor, the other destructors will be skipped.<br />Throwing an exception in the destructor is a fatal error.<br />
  15. 15. __toString()<br />Called whenever you try to use an object in a string context<br />
  16. 16. __toString()<br />
  17. 17. __toString()<br />You can invoke via echo, print, *printf<br />Cast to string<br />$var = (string)$obj;<br />
  18. 18. __toString()<br />__toString() cannot throw an exception – results in a fatal error<br />If you call functions or methods in your __toString() that can throw an exception, make sure you handle it within the __toString() method<br />
  19. 19. __get()<br />Called when code tries to access non-accessible properties<br />
  20. 20. __get()<br />Non-accessible can mean either the property is not defined or that it is not public<br />If it’s not defined, then __get() will be called from both inside and outside of the class context<br />
  21. 21. __get()<br />
  22. 22. __get()<br />
  23. 23. __get()<br />Classes don’t even have to have properties<br />
  24. 24. __get()<br />You could even use it to process values…<br />
  25. 25. __set()<br />Called when code tries to set a property that is not accessible.<br />Not accessible can mean not defined or not public<br />
  26. 26. __set()<br />
  27. 27. __set()<br />If __set() is used to set a property that doesn’t exist, the new property will be public.<br />If it is used on a property that is not public, the accessibility will not change<br />
  28. 28. __set()<br />Use it to validate values as they are passed in<br />
  29. 29. __set()<br />If you want only the values you defined to be used in a class, set that behavior in __set().<br />
  30. 30. __isset()<br />Called when isset() is called on an inaccessible property<br />If you’re using __set() and __get(), not defining __isset() can lead to some really weird errors.<br />
  31. 31. __isset()<br />
  32. 32. __isset()<br />How can this be?<br />You can retrieve the value, but it’s not set?<br />Must define <br />__isset()<br />
  33. 33. __unset()<br />Called when code tries to unset a non-accessible value.<br />Without __unset()<br />
  34. 34. __unset()<br />With __unset()<br />
  35. 35. __call()<br />Called when a method is called that is not accessible.<br />
  36. 36. __call()<br />Use it to extend a class and make everything public…<br />
  37. 37. __call()<br />Yes, I didn’t extend anything in that example, but the principle is the same<br />
  38. 38. __call()<br />Virtual Getters and Setters<br />
  39. 39. __call()<br />All sorts of other uses for __call()<br />
  40. 40. __clone()<br />Used if custom clone() behavior is needed<br />If clone() is called on an object that contains other objects, both the clone and the original contain the same objects.<br />Define __clone() to define what happens when an object is cloned<br />
  41. 41. __clone()<br />Default Clone behavior<br />Using === to show they are the same<br />
  42. 42. __clone()<br />Now with custom __clone()<br />
  43. 43. __clone()<br />In the previous example we are comparing the objects<br />Even if the number value is the same, there are still two different objects<br />
  44. 44. __set_state()<br />Used to set the state of an object when var_export code is used.<br />
  45. 45. __set_state()<br />var_export() creates runnable PHP code.<br />So what happens when we run it?<br />
  46. 46. __set_state()<br />So let’s define it<br />
  47. 47. __set_state()<br />Why the differences?<br />stdClass doesn’t have a __set_state()<br />Since $val is protected, I need a setter.<br />Plus it shows how to deal with non-public stuff<br />
  48. 48. Brief Look at other magic methods<br />You’ve already seen 48 slides, and chances are it’s getting close to an hour…<br />So here’s a quick overview of a few more of the magic methods<br />
  49. 49. __callStatic()<br />New in PHP 5.3<br />Very similar to __call()<br />Intended for when a method is called in a static context (inaccessible method)<br />SomeClass::protectedStaticMethod();<br />
  50. 50. __sleep()<br />Called when an object is serialized with PHP’s serialize.<br />Can be used to shutdown and remove resources (like database connections) which cannot be serialized<br />
  51. 51. __wakeup()<br />Called when an object is unserialized.<br />Can be used to re-establish connections and re-initialize resources<br />ie, reconnect to the database<br />
  52. 52. __invoke()<br />New in PHP 5.3<br />Allows you to treat an object like a function<br />
  53. 53. __autoload()<br />Everyone using PHP 5 should be using this, or at least some form of autoloading.<br />Allows you to automatically load PHP files when they are needed.<br />No more need for require_once or include_once in your scripts<br />
  54. 54. Q & A <br />Any questions?<br />