Presentation is loading. Please wait.

Presentation is loading. Please wait.

Learning OOP in PHP. What is OOP? OOP stands for Object Oriented Programming. OOP is a programming paradigm wherein you create “objects” to work with.

Similar presentations


Presentation on theme: "Learning OOP in PHP. What is OOP? OOP stands for Object Oriented Programming. OOP is a programming paradigm wherein you create “objects” to work with."— Presentation transcript:

1 Learning OOP in PHP

2 What is OOP? OOP stands for Object Oriented Programming. OOP is a programming paradigm wherein you create “objects” to work with.  These objects can then be tailored to your specific needs, to serve different types of applications while maintaining the same code base.

3 What is an object? An object is simply a copy or instance of a “class”. A class can be defined as a “black box” from where we create our objects and access its attributes (variables) and methods (functions).

4 Analogy of Classes You don’t need (and most probably don’t want) to know how the car works when you press the pedals. You just want your car to go back and forth, left and right. And OOP is precisely that. The implementation of “Car” is hidden from us, however we can use its full capacities.

5 Analogy of Classes You don’t need to know how the methods of a class work (if you didn’t implement it yourself), just what they do. OOP is also useful if you have a large number of objects of the same type in a system: you just have to create the objects and then manipulate them all in the same way, without rewriting any code. Moreover, an object is able to maintain it’s state (variable values and such) throughout the execution of the program.

6 OOP in PHP PHP provides every paradigm other “true” OOP languages implement (Python and JAVA, for example), like inheritance, polymorphism and encapsulation.

7 Inheritance The basic idea behind inheritance is that similar objects share common properties. So by creating a “generic” class, we can have a blueprint to build our subsequent classes on. Imagine, if you will, a car’s properties: color, number of wheels, horsepower, number of seats, etc. Having this template we can further specialize our cars by extending this class: creating a racing car that has a “nitro” property, or a truck that has a “trailer” property.

8 Inheritance The bottom line is: create a more generic class that contains most of the common attributes and you will have much less work defining other objects only slightly different. Instead of rewriting the whole code, you just extend it’s properties, saving a lot of time in the process.

9 Encapsulation As previously explained, one of the main advantages of using objects is that we don’t need to reveal all of its members (attributes or functions); just the necessary interfaces to work with it. Details not useful to the use of these objects should be hidden from the rest of the objects. This is what is referred to as encapsulation.

10 Encapsulation Levels of visibility  public: means that a class member is visible and usable / modifiable by everyone  private: means that a class member is only usable / modifiable by the class itself  protected: means that a class member is only usable / modifiable by the class itself and eventual sub-classes NOTE: By default, in PHP, a class member is public unless declared private or protected.

11 Polymorphism Polymorphism is an OOP characteristic that allows the programmer to assign a different meaning or usage to something in different contexts – specifically, to allow a class member to perform different tasks depending on the context it was used. Imagine you have a Person class and two sub- classes of Person: Japanese and American. Both implement a function named talk(), but with different languages and social context. And while both of them are basically People (as they derive from the class Person), their implementation of the function talk() is very different. So you basically have two objects of the class Person in which the talk() function works differently.

12 Sample Code <?php Class Car{ var $color = "RED"; /*public function Car(){ echo "We just created and object."; }*/ public function __construct(){ echo "Car object created. "; } public function goForward(){ echo "Car object go forward. "; } public function goBack(){ echo "Car object go back. "; } public function turnRight(){ echo "Car object turn right. "; } public function turnLeft(){ echo "Car object turn left. "; } public function activateHorn(){ echo "Car object activate horn. "; }

13 Sample Code $mycar = new Car(); $mycar->goForward(); $mycar->goBack(); $mycar->turnRight(); $mycar->turnLeft(); $mycar->activateHorn(); echo "Car object color is ".$mycar->color; echo " "; ?> <?php Class Racecar extends Car{ public function __construct(){ echo "Race Car object created. "; } $myracecar = new Racecar(); $myracecar->goForward(); ?>


Download ppt "Learning OOP in PHP. What is OOP? OOP stands for Object Oriented Programming. OOP is a programming paradigm wherein you create “objects” to work with."

Similar presentations


Ads by Google