Question? Leave a message!




Introduction to object oriented PHP

Introduction to object oriented PHP 4
Introduction to object oriented PHP Marcus Börger eZ publish ConferenceOverview What is OOP PHP and OOP Marcus Börger Introduction to object oriented PHP 2What is OOP class Useless extends Nonsense abstract function blaBla(); Marcus Börger Introduction to object oriented PHP 3;; What does OOP aim to achieve Allow compartmentalized refactoring of code Promote code reuse Promote extensibility, flexibility and adaptability Better for team development Many patterns are designed for OOP Some patterns lead to much more efficient code Do you need to use OOP to achieve these goals Of course not It’s designed to make those things easier though Marcus Börger Introduction to object oriented PHP 4What are the features of OOP Encapsulation Inheritance Polymorphism Marcus Börger Introduction to object oriented PHP 5Encapsulation Encapsulation is about grouping of functionality (operations) and related data (attributes) together into a coherent data structure (classes). Marcus Börger Introduction to object oriented PHP 6Encapsulation Encapsulation is about grouping of functionality (operations) and related data (attributes) together into a coherent data structure (classes). Classes represent complex data types and the operations that act on them. An object is a particular instance of a class. Marcus Börger Introduction to object oriented PHP 7Encapsulation Encapsulation is about grouping of functionality (operations) and related data (attributes) together into a coherent data structure (classes). Classes represent complex data types and the operations that act on them. An object is a particular instance of a class. The basic idea is to recode real life. For instance if you press a key on your laptop keyboard you do not know what is happening in detail. For you it is the same as if you press the keyboard of an ATM. We say the interface is the same. If another person has the same laptop the internal details would be exactly the same. Marcus Börger Introduction to object oriented PHP 8Encapsulation Encapsulation is about grouping of functionality (operations) and related data (attributes) together into a coherent data structure (classes). Classes represent complex data types and the operations that act on them. An object is a particular instance of a class. The basic idea is to recode real life. For instance if you publish a text that is not really different from publishing a picture. Both are content types and you might want to encapsulate the details on how to do the actual publishing in a class. And once you have that you can easily have contend that consists of both pictures and text and yet use the same operations for publishing. Marcus Börger Introduction to object oriented PHP 9Encapsulation: Are Objects Just Dictionaries In PHP 4 objects were little more than arrays. In PHP 5 you get much more control by visibility, interfaces, type hints, interceptors and more. Another difference is coherency. Classes can be told to automatically execute specific code on object creation and destruction. class Simple function construct() /.../ function destruct() /.../ Marcus Börger Introduction to object oriented PHP 10;;;;; Data Hiding Another difference between objects and arrays is that objects permit strict visibility semantics. Data hiding eases refactoring by controlling what other parties can access in your code. public anyone can access it protected only descendants can access it private only you can access it final no one can redeclare it abstract someone else will implement this Why have these in PHP Because sometimes selfdiscipline isn’t enough. Marcus Börger Introduction to object oriented PHP 11;;;;; Inheritance Inheritance allows a class to specialize (or extend) another class and inherit all its methods, properties and behaviors. This promotes Extensibility Reusability Code Consolidation Abstraction Responsibility Marcus Börger Introduction to object oriented PHP 12The Problem of Code Duplication Code duplication contradicts maintainability. You often end up with code that looks like this: function footoxml(foo) // generic stuff // foospecific stuff function bartoxml(bar) // generic stuff // bar specific stuff Marcus Börger Introduction to object oriented PHP 13The Problem of Code Duplication You could clean that up as follows function basetoxml(data) /.../ function footoxml(foo) basetoxml(foo); // foo specific stuff function bartoxml(bar) basetoxml(bar); // bar specific stuff But it’s hard to keep basetoxml() working for the disparate foo and bar types. Marcus Börger Introduction to object oriented PHP 14The Problem of Code Duplication In an OOP style you would create classes for the Foo and Bar classes that extend from a base class that handles common functionality. Sharing a base class promotes sameness. class Base public function toXML() /.../ class Bar extends Base class Foo extends Base public function toXML() public function toXML() parent::toXML(); parent::toXML(); // bar specific stuff // foo specific stuff Marcus Börger Introduction to object oriented PHP 15Polymorphism Suppose a calendar that is a collection of entries. Procedurally dislpaying all the entries might look like: foreach(entries as entry) switch(entry’type’) case 'professional': displayprofessionalentry(entry); break; case 'personal': displaypersonalentry(entry); break; Marcus Börger Introduction to object oriented PHP 16Simplicity through Polymorphism In an OOP paradigm this would look like: foreach(entries as entry) entrydisplay(); The key point is we don't have to modify this loop to add new types. When we add a new type, that type gets a display() method so that it knows how to display itself, and we’re done. Also this is much faster because we do not have to check the type for every element. Marcus Börger Introduction to object oriented PHP 17Simplicity through Magic Actually in PHP you might want this: foreach(entries as entry) echo entry; A class can have a tostring() method which defines how its objects are converted into a textual representation. PHP 5.2 supports this in all string contexts. Marcus Börger Introduction to object oriented PHP 18Another example class Humans public function construct(name) /.../ public function eat() /.../ public function sleep() /.../ public function snore() /.../ public function wakeup() /.../ Marcus Börger Introduction to object oriented PHP 19Some Inheritance class Humans public function construct(name) /.../ public function eat() /.../ public function sleep() /.../ public function snore() /.../ public function wakeup() /.../ class Women extends Humans public function giveBirth() /.../ Marcus Börger Introduction to object oriented PHP 20Inheritance+Polymorphism class Humans public function construct(name) /.../ public function eat() /.../ public function sleep() /.../ public function wakeup() /.../ class Women extends Humans public function giveBirth() /.../ class Men extends Humans public function snore() /.../ Marcus Börger Introduction to object oriented PHP 21A little abstraction abstract class Humans public function construct(name) /.../ abstract public function gender(); public function eat() /.../ public function sleep() /.../ public function wakeup() /.../ class Women extends Humans public function gender() return 'female'; public function giveBirth() /.../ class Men extends Humans public function gender() return 'male'; public function snore() /.../ Marcus Börger Introduction to object oriented PHP 22;; Overloading or Polymorphism the other way round Unlike other languages PHP does not and will not offer overloading polymorphism for method calling. Thus the following will never work in PHP php class Test function toXML(Personal obj) //… function toXML(Professional obj) //… To work around this Use the other way round (call other methods from a single toXML() function in a polymorphic way) Use switch/case (though this is not the OO way) Marcus Börger Introduction to object oriented PHP 23Constructor visibility A protected constructor prevents instantiation class Base protected function construct() class Derived extends Base // constructor is still protected static function getBase() return new Base; // Factory pattern class Three extends Derived public function construct() Marcus Börger Introduction to object oriented PHP 24;; The Singleton pattern Sometimes you want only a single instance of aclass to ever exist. DB connections An object representing the user or connection. class Singleton static private instance; protected function construct() final private function clone() static function getInstance() if(self::instance) self::instance = new Singleton(); return self::instance; a = Singleton::getInstance(); aid = 1; b = Singleton::getInstance(); print bid."\n"; Marcus Börger Introduction to object oriented PHP 25Different Object same behavior Often different objects have the some equal functionality without sharing the same base class class Line function draw() ; class Polygon Line Ellipse protected lines; function draw() foreach(thislines as line) linedraw(); lines Circle Polygon class Rectangle extends Polygon function draw() ; class Ellipse function draw() ; Rectangle class Circle extends Ellipse function draw() parent::draw(); Marcus Börger Introduction to object oriented PHP 26Interfaces Interfaces describe an abstract class protocol Classes may inherit multiple Interfaces interface Drawable Drawable function draw(); class Line implements Drawable function draw() ; class Polygon implements Drawable Line Ellipse protected lines; function draw() foreach(thislines as line) linedraw(); lines Circle Polygon class Rectangle extends Polygon function draw() ; class Ellipse implements Drawable function draw() ; Rectangle class Circle extends Ellipse function draw() parent::draw(); Marcus Börger Introduction to object oriented PHP 27Object to String conversion toString(): semiautomatic object to string conversion with echo and print (automatic starting with 5.2) class Object function toString() return 'Object as string'; o = new Object; echo o; str = (string) o; // does NOT call toString Marcus Börger Introduction to object oriented PHP 28;;;; Typehinting PHP 5 allows to easily force a type of a parameter PHP does not allow NULL for typehints Typehints must be inherited as given in base class PHP 5.1 offers typehinting with arrays PHP 5.2 offers optional typhinted parameters (= NULL) class Object public function compare(Object other) // Some code here public function compare2(other) if (isnull(other) other instanceof Object) // Some code here Marcus Börger Introduction to object oriented PHP 29Class Design It is important to think about your class hierarchy Avoid very deep or broad inheritance graphs PHP only supports isa and hasa relations Tires Bicycle Vehicle Engine Car Bus Truck Diesel Gasoline Tank Turbine Plane Marcus Börger Introduction to object oriented PHP 30Reference Everythining about PHP http://php.net These slides http://talks.somabo.de SPL Documentaion Examples http://php.net/helly/php/ext/spl http://cvs.php.net/phpsrc/ext/spl/examples http://cvs.php.net/phpsrc/ext/spl/internal George Schlossnagle Advanced PHP Programming Andi Gutmans, Stig Bakken, Derick Rethans PHP 5 Power Programming Marcus Börger Introduction to object oriented PHP 31
Website URL
Comment