Seven Steps to Better PHP Code
Upcoming SlideShare
Loading in...5
×
 

Seven Steps to Better PHP Code

on

  • 2,846 views

You have a basic OOP knowledge in PHP? You have seen the usual examples, but don't really see how to apply them to solve your coding problems? This session will show you how you can make better use of ...

You have a basic OOP knowledge in PHP? You have seen the usual examples, but don't really see how to apply them to solve your coding problems? This session will show you how you can make better use of OOP by presenting seven best practices and teach you how to apply them in real life. Topics covered include dependency injection, separation of concerns, loose coupling, and much more.

Statistics

Views

Total Views
2,846
Views on SlideShare
2,844
Embed Views
2

Actions

Likes
5
Downloads
149
Comments
1

1 Embed 2

http://www.slideshare.net 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Seven Steps to Better PHP Code Seven Steps to Better PHP Code Presentation Transcript

  • Stefan Priebsch thePHP.cc ZendCon09 Seven Steps to Better OOP Code
  • Stefan Priebsch Co-Founder and Principal Consultant
  • Premium PHP Consulting & Training. Worldwide. Sebastian Arne Stefan Bergmann Blankerts Priebsch
  • Disclaimer
  • „Hang the rules. They're more like guidelines anyway.“ --Elizabeth Swann, Pirates of the Caribbean
  • Is OOP slow?
  • foo() 3.09 µsec Test::foo() 3.26 µsec $test->foo() 3.12 µsec $test = new Test(); $test->foo() 4.03 µsec The usual disclaimer for benchmarks applies!
  • 25% slower!
  • 1 µsec
  • print ~ 10 µsec file_get_contents() ~ 30 µsec mysql_connect() ~ 100 µsec HTTP GET Request ~ 35,000 µsec The usual disclaimer for benchmarks applies!
  • I/O is where the action is.
  • OOP is fast enough.
  • Do not care about #1 performance * *Some restrictions may apply.
  • What do you do for a living?
  • class Something { public function doWork() { … load data … … perform calculations … … render HTML output … … store calculation result ... } }
  • class Something { public function doManyThings() { $this->loadData(); $this->performCalculations(); $this->renderHtml(); $this->storeResult(); } protected function loadData() protected function performCalulations() protected function renderHtml() protected function storeResult() }
  • class DataLoader { public function loadData() } class Calculator { public function calculateResult() } class HtmlRenderer { public function render() } class StorageManager { public function storeResult() }
  • #2 Clearly separate different concerns
  • One Responsibility
  • class SomeObject { protected function loadData() { $this->data = $this->db->query(...); } public function render() { $this->loadData(); return HtmlRenderer::createTable($this->data); } }
  • Let others do the work.
  • class CrystalBall { public function predictLottoNumbers($a, $b, $c) { … return new LottoNumbers(...); } }
  • #3 Focus on the API
  • Interface vs. Implementation
  • Keep secrets
  • class Person { protected function talk() { Stranger::askForACigarette(); } }
  • class Person { protected function talk() { Stranger::getInstance()->askForACigarette(); } }
  • class Person { protected function talkTo(Friend $friend) { $friend->askForACigarette(); } } class Friend { public function askForACigarette() { return new Cigarette(); } }
  • Do not talk to strangers.
  • Create loosely #4 coupled classes
  • Make dependencies explicit.
  • class SomeObject { protected function loadData() { $this->data = $this->db->query(...); } public function render() { $this->loadData(); return HtmlRenderer::createTable($this->data); } }
  • class SomeObject { protected function loadData() { $this->data = $this->db->query(...); } public function render(Renderer $renderer) { $this->loadData(); return $this->renderer->createTable($this->data); } }
  • class SomeObject { protected function loadData(DbGateway $db) { $this->data = $db->query(...); } public function render(Renderer $renderer) { $this->loadData(); return $renderer->createTable($this->data); } }
  • class SomeObject { public function __construct(DbGateway $db, Renderer $r) { $this->db = $db; $this->renderer = $r; } ... }
  • Use dependency #5 injection
  • class Engine { public function start(); public functoin stop(); public function goFaster($amount); public function goSlower($amount); }
  • class Car extends Engine { ... }
  • class SteeringWheel { public function turnRight($degrees); public function turnLeft($degree); }
  • class Car extends Engine extends SteeringWheel { ... }
  • Multiple inheritance?
  • Engine
  • Engine + Steering Wheel
  • Engine + Steering Wheel + Seat
  • Combine objects.
  • Seat Car Steering Engine Wheel
  • class Car { protected $engine; protected $steeringWheel; protected $frontSeat; public function __construct() { $this->engine = new Engine(); $this->steeringWheel = new SteeringWheel(); $this->frontSeat = new Seat(); } }
  • class Car { public function __construct(Engine $engine, SteeringWheel $steeringWheel, Seat $seat) { $this->engine = $engine; $this->steeringWheel = $steeringWheel; $this->frontSeat = $seat; } }
  • $engine = new Engine(); $steeringWheel = new SteeringWheel(); $seat = new Seat(); $car = new Car($engine, $steeringWheel, $seat);
  • $engine = new SuperStrongEngine(); $steeringWheel = new FancySteeringWheel(); $seat = new MichaelSchuhmacherRacingSeat(); $car = new Car($engine, $steeringWheel, $seat);
  • Favour composition #6 over inheritance
  • Avoid inheritance.
  • Constants Superglobals Session Class to Test Webservice Configuration Database
  • If it's not tested, it does not exist
  • Make it easy #7 to (unit) test
  • Fewer dependencies.
  • Testable = Maintainable
  • Testable = Extensible
  • Thank you.
  • Contact ■ http://thePHP.cc ■ http://www.priebsch.de ■ http://www.slideshare.net/spriebsch ■ http://twitter.com/spriebsch ■ stefan@thePHP.cc, IRC: spriebsch Copyright © 2009 thePHP.cc, Germany