Advanced PHP Simplified
-
Upload
mark-niebergall -
Category
Software
-
view
119 -
download
0
Transcript of Advanced PHP Simplified
About Mark Niebergall
● Developing in PHP since 2005● Masters degree in Information Systems● Senior Software Engineer and Team Lead● Occupational health screening project● Security: SSCP, CSSLP Certified● PHP, databases, JavaScript● Enjoy being outdoors
Disclaimer
● Breadth not depth● Lots of other cool stuff● Think about your current coding style and
architecture● Set goals for professional development
Abstract Class
● Usage: abstract class X..., Y extends X● Abstract method: abstract public function
doSomething($a);● Abstract method signatures must match● Cannot instantiate an abstract class
Abstract Class
● Can have methods with functionality● Can have class properties● Can extend another abstract class● Represents an idea or base concept
Abstract Uses
● X "is a" Y (NOT "has a")● Composition over Inheritance● Centralizing logic● Reducing duplicate code● Avoid a maze - keep code readable
Interface
● User Interface● System Interface● Provides a way for communication or
control● Connect two things together
Interface
● Usage: interface X, Y implements X● Methods must be implemented● Method signatures must match● Methods are left empty
Interface Uses
● When no logic is required but methods
signatures can be reused● Contract between concept and
implementation● Class can implement multiple interfaces● Polymorphism
Trait
● PHP 5.4● Usage: trait X {...}, use X● Can have methods and properties● "Looks" like a concrete class● Horizontal Inheritance● Cannot be instantiated
Trait Usage
● Common attributes and functionality● Keep vertical inheritance in mind,
especially when making changes● Reduces some limitations of single
inheritance● Consider: required for functionality within
this class?
Namespace
● PHP 5.3● Usage: namespace X; use Y; new Y\Z;
new \Other\W;● Grouping related classes together
Namespace Uses
● Shorten_Up_Really_Long_Class_Names_
In_Your_Application● Naming collisions● Consider an autoloader with file structure
built around namespaces
Namespace Uses
● Watch out for keywords; example: Trait,
Case, Switch● Must be first code in file● Aliasing: use Xyz as X;
Dependency Injection
● Send object dependencies into an object
from outside● Pass service into client rather than client
building service
Dependency Injection
● Injection by either constructor, setter, or
interface● Big help for unit tests, mock objects● Does add more lines of code● Composition over Inheritance
Dependency Injection Uses
● Constructor: __construct(X $x, ...)● Setter: setX(X $x) { $this->x = $x; }● Interface: setX(X $x);● Service locator● See PHPUnit documentation for getMock,
getMockBuilder, getMockForAbstractClass
Dependency Injection Examples
● Desktop computer parts from different
manufacturers● Lamp light bulbs: colors, types
Things to Consider
● Why need for abstract vs interface vs
trait?● Benefits of vertical vs horizontal
inheritance?● Traits vs dependency injection?● Tradeoffs of complexity vs code reuse
(think of database normalization)
Conclusion
● Goals for using concepts● Evaluate current architectural approach● Just scratched the surface on topics
covered● Consider how concepts all come together
for solution