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.

Matt Land

May 15, 2015
Tweet

More Decks by Matt Land

Other Decks in Technology

Transcript

  1. 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
  2. Interfaces Predefined: Traversable*, Iterator, IteratorAggregate, ArrayAccess, Serializable, Closure, Generator, Count

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

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

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

    pairs Manually skip over in callbacks debug_backtrace()
  6. __construct __destruct When: After ram is allocated Use: Lots *****

    Before garbage collect Log if a class finishes **
  7. __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 *
  8. __isset __unset called when isset() is called on an inaccessible

    property Property Overloader * called when empty() is called on an inaccessible property Property Overloader *
  9. __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 **
  10. __sleep __wakeup When: serialize() is called. Intended for db connect/disconnect

    ** unserialize() is called Was useful for testing before reflection methods. **
  11. __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.
  12. __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
  13. __invoke When: A class is called as a function. ***

    $entity = new entity(); $entity(3);
  14. 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