Successfully reported this slideshow.
Your SlideShare is downloading. ×

Advanced PHP Simplified

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad

Check these out next

1 of 64 Ad

Advanced PHP Simplified

Download to read offline

Advanced PHP Simplified

Increase your PHP skills and improve your architecture as you learn why, when, and how to use advanced PHP features. Abstracts, interfaces, traits, namespaces, and dependency injection will each be discussed with code examples to help make using them simple. All these features will then be brought together to demonstrate clean coding. This will be a perfect presentation to help you sharpen your skill-set to include these tools to make your code even more clean, structured, and awesome!

Advanced PHP Simplified

Increase your PHP skills and improve your architecture as you learn why, when, and how to use advanced PHP features. Abstracts, interfaces, traits, namespaces, and dependency injection will each be discussed with code examples to help make using them simple. All these features will then be brought together to demonstrate clean coding. This will be a perfect presentation to help you sharpen your skill-set to include these tools to make your code even more clean, structured, and awesome!

Advertisement
Advertisement

More Related Content

Advertisement

Similar to Advanced PHP Simplified (20)

More from Mark Niebergall (20)

Advertisement

Recently uploaded (20)

Advanced PHP Simplified

  1. 1. Advanced PHP Simplified Mark Niebergall https://joind.in/talk/e328e
  2. 2. Thank You Sponsors
  3. 3. About Mark Niebergall ● PHP since 2005 ● Masters degree in MIS ● Senior Software Engineer ● Drug screening project ● UPHPU President ● SSCP, CSSLP Certified and SME ● Drones, fishing, skiing, father, husband
  4. 4. Advanced PHP Simplified
  5. 5. Disclaimer ● Breadth not depth ● Think about your current coding style and architecture ● Leverage topics covered
  6. 6. Topics to Cover ● Abstract ● Interface ● Trait ● Namespace ● Dependency Injection
  7. 7. Abstract
  8. 8. Abstract ● Abstract Art ● Abstract of text ● Abstract thought or idea
  9. 9. Abstract Class ● abstract class X {...} ● class Y extends X {...} ● Cannot instantiate an abstract class
  10. 10. Abstract Class ● abstract public function doSomething($a); ● protected function somethingElse() {...} ● Abstract method signatures must match
  11. 11. Abstract Class ● Can have methods with functionality ● Can have class properties ● Can extend another abstract class ○ Classes can extend concrete classes
  12. 12. Abstract Uses ● X "is a" Y (NOT "has a")
  13. 13. Abstract Uses ● Represents an idea or base concept ● Composition over Inheritance ● Centralizing logic ● Reducing duplicate code ● Avoid a maze - keep code readable
  14. 14. Abstract Examples ● Truck extends Vehicle ● Laptop extends Computer ● Email extends Communication
  15. 15. Abstract Examples ● abstract Animal ● abstract Mammal extends Animal ● abstract Canine extends Mammal ● abstract Dog extends Canine ● class GermanSheperd extends Dog
  16. 16. abstract class Pen { protected $length; abstract public function usePen(); abstract public function refuel(); protected function setLength($length) { $this->length = (float) $length; return $this; } public function getLength() { return $this->length; } }
  17. 17. abstract class Bird { protected $beak; abstract public function walk(); abstract private function eat(); protected function fly(Wings $wings) { $wings->flap(); } }
  18. 18. Abstract
  19. 19. Interface
  20. 20. Interface ● User Interface ● System Interface ● Provides a way for communication or control ● Connect two things together
  21. 21. Interface ● interface X { public function doStuff(Thing $thing); protected function other(); } ● class Y implements X {...} ● abstract Z implements V, W, X {...}
  22. 22. Interface ● Methods must be implemented ● Method signatures must match ● Methods are left empty in the interface
  23. 23. Interface Uses ● When no logic is required but methods signatures can be reused ● Contract between concept and implementation
  24. 24. Interface Uses ● Class can implement multiple interfaces ● Polymorphism
  25. 25. Interface Examples ● Auditing ● Underwater Creatures ● USB
  26. 26. Interface Examples ● Authentication methods ● Responses to a request ● Orders from vendors and customers
  27. 27. interface Light { public function shine(); public function travel($x, $y, $z); }
  28. 28. interface ProcessSomething { public function process(Something $something); }
  29. 29. Interface
  30. 30. Traits
  31. 31. Traits ● Characteristics of something ● Attributes possessed
  32. 32. Trait ● trait X { public $property; protected function doStuff() {...} } ● class Something { use X; }
  33. 33. Trait ● PHP 5.4+ ● Can have methods and properties ● "Looks" like a concrete class ● Horizontal Inheritance ● Cannot be instantiated
  34. 34. Trait Usage ● Common attributes and functionality ● Consider: required for functionality within this class?
  35. 35. Trait Usage ● Keep vertical inheritance in mind, especially when making changes ● Reduces some limitations of single inheritance
  36. 36. Trait Examples ● Car tire ● Underwater creature
  37. 37. trait Circular { protected $radius; public function getDiameter() { return bcmul($this->radius, 2); } public function getArea() { return bcmul(pi(), bcpow($this->radius, 2)); } }
  38. 38. trait Wave { protected $length; protected $amplitude; protected $frequency; public function setLength($length) { $this->length = (float) $length; return $this; } public function getLength() { return $this->length; } public function setAmplitude($amplitude) { $this->amplitude = (float) $amplitude; return $this; } public function getAmplitude() { return $this->amplitude; } }
  39. 39. Trait
  40. 40. Namespaces
  41. 41. Namespaces ● Container for identification ● Encapsulate items
  42. 42. Namespace ● namespace DomainTopicSubgroup; use OtherAreaZ; class A { protected function doStuff() { $z = new Z; } }
  43. 43. Namespace ● PHP 5.3+ ● Grouping related classes together
  44. 44. Namespace Uses ● Shorten_Up_Really_Long_Class_Names_ In_Your_Application ● Avoid naming collisions
  45. 45. Namespace Uses ● Autoloader with file structure built around namespaces ● Can have multiple ● Often one for composer, one for project
  46. 46. Namespace Uses class Autoloader { public static function registerAutoloader() { spl_autoload_register(array('self', 'loader'), false); } public static function loader($className) { require __DIR__ . ‘/../../’ . $className . ‘.php’; } } require_once __DIR__ . ‘/../vendor/autoload.php’; Autoloader::registerAutoloader();
  47. 47. Namespace Uses ● Must be first code in file ● Aliasing: use WidgetXyz as X; ● Watch out for keywords ○ Trait ○ Case ○ Switch
  48. 48. Namespace Uses ● Avoid collisions ○ use BobCat as BobCat; ○ use JaneCat as JaneCat;
  49. 49. Namespace Examples ● Last name ○ namespace Smith; ○ class John {...}
  50. 50. Namespace Examples ● Just_About_Any_Long_Name ○ namespace JustAboutAnyLong; ○ class Name {...}
  51. 51. Namespace Examples Domain/Subject/Science.php: <?php namespace DomainSubject; use OtherDomainSubjectThing; use OtherDomainArea; class Science { protected function doSomething(Thing $thing) { $thing->doWhatItDoes(); } public function otherStuff(AreaNorth $north) { $north->head(AreaSouth::getPole()); } }
  52. 52. Namespace
  53. 53. Dependency Injection ● Send object dependencies into an object from outside ● Pass class as parameter into a method rather than method building the class
  54. 54. Dependency Injection Uses ● Constructor: __construct(X $x, ...) ● Setter: setX(X $x) { $this->x = $x; } ● Interface: setX(X $x); ● Method: doSomething(Thing $thing) {...}
  55. 55. Dependency Injection ● Big help for unit tests ● Allows for use of mock objects ● See PHPUnit documentation for getMockBuilder, getMockForAbstractClass, and other PHPUnit methods
  56. 56. Dependency Injection ● Does add more lines of code ● Composition over Inheritance
  57. 57. Dependency Injection Uses ● Service locator ● Factories
  58. 58. Dependency Injection Examples ● Desktop computer parts from different manufacturers ● Lamp light bulbs: colors, types
  59. 59. namespace Pen; use LightLight as LightLaser; require_once __DIR__ . '/../Autoloader.php'; Autoloader::registerAutoloader(); class Laser extends Pen { protected $lightLaser; public function __construct(LightLaser $lightLaser = null) { $this->lightLaser = $lightLaser; } protected function getLightLaser() { if (is_null($this->lightLaser)) { $this->lightLaser = new LightLaser; } return $this->lightLaser; } public function usePen() { $this->getLightLaser()->shine(); } public function refuel() { $this->getLightLaser()->replaceBatteries(); } }
  60. 60. Dependency Injection
  61. 61. Things to Consider ● Why need for abstract vs interface vs trait? ● Benefits of vertical vs horizontal inheritance?
  62. 62. Things to Consider ● Traits vs dependency injection? ● Tradeoffs of complexity vs code reuse (think of database normalization)
  63. 63. Conclusion ● Goals for using concepts ● Evaluate current architectural approach ● Just scratched the surface on topics covered ● Consider how concepts all come together for solution
  64. 64. Questions? ● https://joind.in/talk/e328e

×