34
Advanced PHP Simplified Mark Niebergall https://joind.in/13998

Advanced PHP Simplified

Embed Size (px)

Citation preview

Advanced PHP SimplifiedMark Niebergall

https://joind.in/13998

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

Topics to Cover

● Abstract● Interface● Trait● Namespace● Dependency Injection

Disclaimer

● Breadth not depth● Lots of other cool stuff● Think about your current coding style and

architecture● Set goals for professional development

Abstract

● Abstract Art● Abstract of text● Abstract thought or idea

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

Abstract Examples

● Car● Mammal, Fish, or Bird● Computer

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

Interface Examples

● Auditing● Underwater Creatures● USB

Traits

● Characteristics of something● Attributes possessed

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?

Trait Examples

● Car tire● Underwater creature

Namespaces

● Container for identification● Encapsulate items

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;

Namespace Examples

● Last name● Just_About_Any_Long_Name to just

Long\Name● Bob\Cat and Jane\Cat

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

To The Example

● Laser pen

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

Questions?

● https://joind.in/13998