Created by Juan Manuel Torres / @onema / onema.io
Who knows what OOP is?
Wikipedia: Object-oriented programming (OOP) is a programming paradigm that represents concepts as "objects" that have data fields (attributes that describe the object) and associated procedures known as methods.
class ClassName
{
// These are variables bound to the object and can
// only be accessed by the object
public $classProperty01;
public function classMethod01()
{
// Methods are class specific function.
// Objects can reference themselves using $this.
}
}
Set of special methods that are called when certain actions occur with in objects.
Constructors and Destructors are examples
Classes can extend the behaviour of other classes by using the "extends" keyword
class Animal
{
protected $sound;
public function setSound($sound)
{
$this->$sound = $sound;
}
}
class Dog extends Animal
{
protected $trick;
public function setTrick(TrickClass $trickObject)
{
$this->$trick = $trickObject;
}
}
class Animal
{
protected $sound;
public function setSound($sound)
{
$this->$sound = $sound;
}
}
class Dog extends Animal
{
protected $trick;
public function setSound($sound)
{
parent::setSound($sound)
// assume DoggyTalk extends TrickObject
$trick = new DoggyTalk($sound);
$this->setTrick($trick)
}
// . . .
}
class Animal
{
protected $sound;
public function setSound($sound)
{
$this->$sound = $sound;
}
}
class Dog extends Animal
{
protected $trick;
public function setSound($sound)
{
parent::setSound($sound)
// assume DoggyTalk extends TrickObject
$trick = new DoggyTalk($sound);
$this->setTrick($trick)
}
// . . .
}
class Dog extends Animal
{
protected $trick;
public function setSound($sound, DoggyTalk $trick)
{
parent::setSound($sound)
$this->setTrick($trick)
}
// . . .
}
Violates Liskov Substitution Principle
Describes a pattern in which classes have different behaviour but share common interface.
As developers we are in charge of creating the software that will display new 3D models and simulations.
As part of improving the software, we should be able to create 4WD vehicles. This is the first of many requirements that will come from management.
What is wrong with this?
There are part-time, automatic, shift on the fly 4WD, 2WD and All-Wheel Drive!
We could just overwrite the behaviour of things that change.
Exercise 1: In groups of 2 discus and improve the current class design.
HINT: use polymorphismIdentify what parts of the application changes and separate them from what stays the same.
Exercise 2:Improve your own design.
HINT: use polymorphismProgram to an interface, not an implementation.
Favor composition over inheritance.
abstract class Automobile
{
protected $wheelDriveBehavior;
protected $honkBehavior;
public function wheelDrive()
{
$this->wheelDriveBehavior->wheelDrive();
}
public function honk()
{
$this->honkBehavior->honk();
}
}
class Truck extends Automobile
{
public function __construct(WheelDriveInterface $wheelDrive, HonkInterface $honk)
{
//$this->wheelDriveBehavior = new 4WheelDrive();
//$this->honkBehavior = new LaCucarachaHonk();
$this->wheelDriveBehavior = $wheelDrive;
$this->honkBehavior = $honk;
}
// other methods ...
}