Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Deep Into PHP Classes

Deep Into PHP Classes

PHP classes incur a huge memory footprint vs a global function. What do we gain for that overhead? A glance at the PHP source code reveals the “magic methods” built into every class. Beyond ‘construct’ and ‘toString’ are a full suite of useful methods, and the built in special interfaces that compliment them. Learn to make your classes do more with less code, as well as leverage other language constructs like traits and generators.

973e0861866767b3940a6dbb5aac3642?s=128

Matt Land

May 15, 2015
Tweet

More Decks by Matt Land

Other Decks in Technology

Transcript

  1. Deep into PHP Classes Interfaces, Traits, Magic Methods & Generators

  2. PHP: leaving 5.3 5.4: Array shorthand syntax, Traits $array =

    [1, 3, 2, [1, 5]]; $array = [“first” => 2, [“2nd” => 3, “3rd” => 2]]; 5.5: Generators, try/catch/finally 5.6: Constant Scalar Expressions $this->self::CONST_DEF; //OMG <3
  3. Interfaces Predefined: Traversable*, Iterator, IteratorAggregate, ArrayAccess, Serializable, Closure, Generator, Count

    Implemented: /Interface/Predefined/Count.php /Interface/Predefined/Iterator.php
  4. Interfaces For Better Parameter Handling /MagicMethods/toString/parameterHandling. php For Expected Methods

    /MagicMethods/toString/depencyInjection.php For Dependency Injection /Interface/DependencyInjection/Testable.php
  5. Traits: Multiple Inheritance kinda Rules and Resolution: /Traits/myConflict.php Real example:

    Logging /Traits/myThing.php Real example: Settings /Traits/mySettings.php
  6. Magic Methods Double underscore Interact with class dispatch Usually in

    pairs Manually skip over in callbacks debug_backtrace()
  7. Pairs __construct __call __set __isset __sleep __destruct __callStatic __get __unset

    __wakeup
  8. Singles __toString __clone __invoke __set_state

  9. __construct __destruct When: After ram is allocated Use: Lots *****

    Before garbage collect Log if a class finishes **
  10. __call __callStatic When: $this-> call that does not resolve to

    any other class method Method Overloader Use: Dynamic ORM mapping jQuery style getter/setter *** self:: or static:: call that does not resolve to any other static class method Method Overloader Obfuscated coding competitions *
  11. __isset __unset called when isset() is called on an inaccessible

    property Property Overloader * called when empty() is called on an inaccessible property Property Overloader *
  12. __get __set Get public properties that are not set $var

    = $myObj->tvar; Property Overloader ** Set public properties that are not set $myObj->tvar = $var; Property Overloader **
  13. __sleep __wakeup When: serialize() is called. Intended for db connect/disconnect

    ** unserialize() is called Was useful for testing before reflection methods. **
  14. __toString When: An object is cast to a string. echo

    myObject; ***** Use: Unit testing and debugging. Not a good idea to rely on for user display.
  15. __clone When: clone() is called on an object. echo myObject;

    *** Use: Unit testing and debugging. Extend concrete class, override normal clone to change privacy of properties
  16. __invoke When: A class is called as a function. ***

    $entity = new entity(); $entity(3);
  17. __set_state When: var_export is called on class. * Expect depreciation

  18. Bonus: register_shutdown_function When: script termination or exit(), unless out of

    memory error. Does call on other Fatals Use: micro-MVC with Obstart/Obflush