Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Interfaces Predefined: Traversable*, Iterator, IteratorAggregate, ArrayAccess, Serializable, Closure, Generator, Count Implemented: /Interface/Predefined/Count.php /Interface/Predefined/Iterator.php

Slide 4

Slide 4 text

Interfaces For Better Parameter Handling /MagicMethods/toString/parameterHandling. php For Expected Methods /MagicMethods/toString/depencyInjection.php For Dependency Injection /Interface/DependencyInjection/Testable.php

Slide 5

Slide 5 text

Traits: Multiple Inheritance kinda Rules and Resolution: /Traits/myConflict.php Real example: Logging /Traits/myThing.php Real example: Settings /Traits/mySettings.php

Slide 6

Slide 6 text

Magic Methods Double underscore Interact with class dispatch Usually in pairs Manually skip over in callbacks debug_backtrace()

Slide 7

Slide 7 text

Pairs __construct __call __set __isset __sleep __destruct __callStatic __get __unset __wakeup

Slide 8

Slide 8 text

Singles __toString __clone __invoke __set_state

Slide 9

Slide 9 text

__construct __destruct When: After ram is allocated Use: Lots ***** Before garbage collect Log if a class finishes **

Slide 10

Slide 10 text

__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 *

Slide 11

Slide 11 text

__isset __unset called when isset() is called on an inaccessible property Property Overloader * called when empty() is called on an inaccessible property Property Overloader *

Slide 12

Slide 12 text

__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 **

Slide 13

Slide 13 text

__sleep __wakeup When: serialize() is called. Intended for db connect/disconnect ** unserialize() is called Was useful for testing before reflection methods. **

Slide 14

Slide 14 text

__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.

Slide 15

Slide 15 text

__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

Slide 16

Slide 16 text

__invoke When: A class is called as a function. *** $entity = new entity(); $entity(3);

Slide 17

Slide 17 text

__set_state When: var_export is called on class. * Expect depreciation

Slide 18

Slide 18 text

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