Object oriented programming in PHP ppt

Introduction to object oriented PHP and object oriented programming concepts in php ppt and php object oriented programming tutorial ppt
NicolusNorton Profile Pic
NicolusNorton,Germany,Professional
Published Date:08-07-2017
Your Website URL(Optional)
Comment
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 re-use 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 re-code 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 re-code 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 re-declare it abstract someone else will implement this Why have these in PHP? Because sometimes self-discipline 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 foo_to_xml(foo) // generic stuff // foo-specific stuff function bar_to_xml(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 base_to_xml(data) /.../ function foo_to_xml(foo) base_to_xml(foo); // foo specific stuff function bar_to_xml(bar) base_to_xml(bar); // bar specific stuff But it’s hard to keep base_to_xml() 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': display_professional_entry(entry); break; case 'personal': display_personal_entry(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) entry-display(); 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 20