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!
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
9. Abstract Class
● abstract class X {...}
● class Y extends X {...}
● Cannot instantiate an abstract class
10. Abstract Class
● abstract public function doSomething($a);
● protected function somethingElse() {...}
● Abstract method signatures must match
11. Abstract Class
● Can have methods with functionality
● Can have class properties
● Can extend another abstract class
○ Classes can extend concrete classes
13. Abstract Uses
● Represents an idea or base concept
● Composition over Inheritance
● Centralizing logic
● Reducing duplicate code
● Avoid a maze - keep code readable
15. Abstract Examples
● abstract Animal
● abstract Mammal extends Animal
● abstract Canine extends Mammal
● abstract Dog extends Canine
● class GermanSheperd extends Dog
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. abstract class Bird
{
protected $beak;
abstract public function walk();
abstract private function eat();
protected function fly(Wings $wings)
{
$wings->flap();
}
}
20. Interface
● User Interface
● System Interface
● Provides a way for communication or
control
● Connect two things together
21. Interface
● interface X {
public function doStuff(Thing $thing);
protected function other();
}
● class Y implements X {...}
● abstract Z implements V, W, X {...}
22. Interface
● Methods must be implemented
● Method signatures must match
● Methods are left empty in the interface
23. Interface Uses
● When no logic is required but methods
signatures can be reused
● Contract between concept and
implementation
53. Dependency Injection
● Send object dependencies into an object
from outside
● Pass class as parameter into a method
rather than method building the class
55. Dependency Injection
● Big help for unit tests
● Allows for use of mock objects
● See PHPUnit documentation for
getMockBuilder,
getMockForAbstractClass, and other
PHPUnit methods
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();
}
}
61. Things to Consider
● Why need for abstract vs interface vs
trait?
● Benefits of vertical vs horizontal
inheritance?
62. Things to Consider
● Traits vs dependency injection?
● Tradeoffs of complexity vs code reuse
(think of database normalization)
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