Intermediate oop in php
-
Upload
david-stockton -
Category
Technology
-
view
350 -
download
6
description
Transcript of Intermediate oop in php
![Page 1: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/1.jpg)
Intermediate OOP in PHP
PHP World - November 13, 2014 David Stockton
@dstockto
![Page 2: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/2.jpg)
What we’ll cover
Magic Methods
Polymorphism
Collections
Filtering Collections
SPL Iterator
![Page 3: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/3.jpg)
What we’ll cover
SPL Count
SPL ArrayAccess
Listener Pattern
Loose Coupling
High Cohesion
![Page 4: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/4.jpg)
Magic Methods
Methods all start with __
![Page 5: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/5.jpg)
__construct & __destruct
__construct is called when you new up an object
Initialize object properties
Don’t do work!
__destruct is called when an object goes out of scope
Shut down connections, cleanup work
![Page 6: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/6.jpg)
__call
__call($name, array $arguments)
Method called when method is called that is not in scope
Method doesn’t exist
Method is private / protected and called from outside object
![Page 7: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/7.jpg)
__call
![Page 8: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/8.jpg)
__callStatic
Just like __call but for static calls out of scope
Foo::createFromThing()
![Page 9: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/9.jpg)
__get, __set, __isset, __unset
In many cases, if you need one, you need them all
Weird bugs if you don’t implement them all
![Page 10: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/10.jpg)
__get
__get($name)
Called when reading an object property that’s not in scope
!
$value = $foo->madeUpValue;
![Page 11: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/11.jpg)
__get
![Page 12: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/12.jpg)
__set
__set($name, $value)
!
!
![Page 13: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/13.jpg)
So, why do we need them all?
$foo->bar = ‘baz’;
$value = $foo->bar; // Get baz
if (isset($foo->bar)) { $value = $foo->bar; // Never get here if no __isset }
![Page 14: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/14.jpg)
__isset
__isset($name)
isset($object->variable)
!
!
![Page 15: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/15.jpg)
__unset
__unset($name)
unset($object->variable);
!
!
![Page 16: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/16.jpg)
What you can do with them
Implement a class that doesn’t allow dynamic values
Implement a class that only allows values with certain naming conventions
Automatic calling of accessors (getters and setters)
![Page 17: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/17.jpg)
The downsides
Harder to follow, no “ctrl-click” in IDEs
No auto-complete for __call or __callStatic methods in IDEs
Can use @method doc block to help
@method int add() add(numeric $a, numeric $b) Add numbers
![Page 18: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/18.jpg)
@method docblock
![Page 19: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/19.jpg)
__sleep & __wakeup
Called on serialize() and unserialize() calls
__sleep: Use to close connections, cleanup
__wakeup: Use to re-establish connections, setup
Why?
Some things in PHP are not serializable
![Page 20: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/20.jpg)
__toString()
Retrieve string representation of a class
Must return a string
Cannot throw an exception
![Page 21: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/21.jpg)
__toString
![Page 22: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/22.jpg)
__toString
![Page 23: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/23.jpg)
__clone
Create a new object based on an existing object
Called when clone is used:
$foo = clone $bar;
![Page 24: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/24.jpg)
__clone
By default, clone is shallow
!
!
!
![Page 25: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/25.jpg)
$foo = new Foo();
Foo$foo
Foo $bar$foo Bar
![Page 26: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/26.jpg)
$foo2 = clone $foo;
Foo $bar$foo Bar
Foo $bar$bar
![Page 27: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/27.jpg)
Now with __clone
![Page 28: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/28.jpg)
$foo = new Foo();
Foo$foo
Foo $bar$foo Bar
![Page 29: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/29.jpg)
$foo2 = clone $foo;
Foo $bar$foo Bar
Foo $bar$bar
__clone called on the clone
![Page 30: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/30.jpg)
$foo2 = clone $foo;
Foo $bar$foo Bar
Foo $bar$bar Bar
![Page 31: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/31.jpg)
__invoke
Allows an object to be used like a function or callable
Object can be built configured and then “executed”
![Page 32: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/32.jpg)
__invoke
![Page 33: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/33.jpg)
__invoke
![Page 34: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/34.jpg)
__autoload
Don’t use __autoload, use spl_autoload_register
Called when a class, interface, or trait is used that hasn’t been loaded
Allows just in time loading of classes, no more requires and includes at the top of files
![Page 35: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/35.jpg)
__set_state
Used with var_export
Allows setting of object variables from an array of input
![Page 36: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/36.jpg)
__debugInfo
New in PHP 5.6
Controls output of var_dump
![Page 37: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/37.jpg)
__debugInfo
![Page 38: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/38.jpg)
var_dump
![Page 39: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/39.jpg)
Add __debugInfo
![Page 40: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/40.jpg)
var_dump again
![Page 41: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/41.jpg)
PolymorphismAllow code to use different objects in the same
way
![Page 42: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/42.jpg)
Speakableinterface
![Page 43: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/43.jpg)
Polymorphism
![Page 44: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/44.jpg)
Polymorphism
![Page 45: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/45.jpg)
Collections and Polymorphism
![Page 46: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/46.jpg)
Silverware Drawer
![Page 47: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/47.jpg)
SilverwareContainerinterface
![Page 48: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/48.jpg)
Silverwareinterface
![Page 49: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/49.jpg)
Abstract Utensil Class
![Page 50: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/50.jpg)
A Forking Example
![Page 51: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/51.jpg)
Silver Spoons
![Page 52: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/52.jpg)
Knife
![Page 53: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/53.jpg)
Put silverware away
![Page 54: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/54.jpg)
How many utensils?
Add method getNumberOfItems
!
!
Implement SPL Countable
- OR -
![Page 55: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/55.jpg)
SPL Countable
![Page 56: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/56.jpg)
How to use?
![Page 57: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/57.jpg)
Retrieve Specific Utensils
![Page 58: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/58.jpg)
SPL to the Rescue
Implement SPL ArrayAccess
Make our object work like an array
![Page 59: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/59.jpg)
SPL ArrayAccess
![Page 60: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/60.jpg)
SPL ArrayAccess
![Page 61: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/61.jpg)
What does that allow?
![Page 62: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/62.jpg)
Use an iterator: !
• Implement iterator • Extend one of these: • AppendIterator • ArrayIterator • CachingIterator • CallbackFilterIterator • InfiniteIterator • …etc.
Loop Over Utensils in Drawer
![Page 63: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/63.jpg)
Easy wayExtend ArrayIterator
![Page 64: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/64.jpg)
Add some silverware
![Page 65: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/65.jpg)
Now what? Iterate it!
![Page 66: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/66.jpg)
A new iterator
![Page 67: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/67.jpg)
Only get clean utensils
![Page 68: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/68.jpg)
SPLSubject / SPLObserverMoving right along
![Page 69: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/69.jpg)
Implement SplSubject
![Page 70: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/70.jpg)
Email class continued
![Page 71: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/71.jpg)
Validate uses our observers
![Page 72: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/72.jpg)
Build some observersInvalid Emails don’t send
![Page 73: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/73.jpg)
Limit Sends by domains?
![Page 74: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/74.jpg)
Put it all together
![Page 75: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/75.jpg)
Loose Coupling
![Page 76: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/76.jpg)
Loose Coupling
Minimize number of hard-coded dependencies
Type hint interfaces, not classes
Dependency Injection / Inversion of Control
![Page 77: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/77.jpg)
Dependency Injection Simply
![Page 78: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/78.jpg)
![Page 79: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/79.jpg)
High CohesionDo one thing well
![Page 80: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/80.jpg)
High Cohesion
Do one thing well
Methods
Classes
Packages / Namespaces
Everything the class does is related to the goal
![Page 81: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/81.jpg)
–David Stockton
“If you have to use ‘and’ to describe what your method or class does,
it’s probably doing too much.”
![Page 82: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/82.jpg)
Prefer Composition over Inheritance
![Page 83: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/83.jpg)
Prefer Composition over Inheritance
Inheritance is not the only way to expand functionality of a class
Often it’s not even the best way
![Page 84: Intermediate oop in php](https://reader038.fdocuments.in/reader038/viewer/2022102723/558a3911d8b42aaa1d8b465b/html5/thumbnails/84.jpg)
Photo Credits
• Lego Magician - https://www.flickr.com/photos/evaysucamara/5438832695 • Silverware - https://www.flickr.com/photos/jenny-pics/4128947072 • Fork - https://www.flickr.com/photos/naturesdawn/2507321223 • Trailer hitch - https://www.flickr.com/photos/jeremybrooks/2476396242 • The Glue Side of the Force - https://www.flickr.com/photos/st3f4n/4085958000 • Last Will and Testament - https://www.flickr.com/photos/ken_mayer/5599532152 • Guest Composer - https://www.flickr.com/photos/tamuc/12843465194