paradigm • PHP is a multi-‐paradigm programming language • OO is the most popular paradigm today and it's used in most of the languages • Java • C# • C++ • Swift ...
Management • Better management by coding classes and objects that communicate with each other • Maintanence • When changing code, it does not influence the whole app
of objects: • cars, buildings, trees, ships, humans, flowers.. • Every object has actions (=methods!) that can incluence other objects • jack drives ferrari. • Object jack has a method drive that influences some way to object ferrari. • In OO, you should implement the app so that it consists of objects that influence each other!
• Datsun 100A has different actions or methods: • drive • brake • park... • Datsun 100A has information or attributes: • color • amount of gears • amount of doors...
– class to PHP class Person { public $firstname; public $lastname; public $age; public $profession; public $phonenumber; function eat() { print "Eating"; } function sleep() { print "Sleeping"; } function drinkBeer() { print "Drinking Beer"; } }
The reason for this is that other objects cannot change the values as they will • You don't for example want that every object in the world can change person's weight to 500kg...
passed by value • When doing this on basic types, like ints: • $a = $b; // $b integer value is copied to $a • Objects are passed by reference • When doing this on objects • $a = $b; // $b memory address is copied to $a
when an object is created • PHP 5 does not provide default constructor, like in other languages • Constructor method has a special name in PHP • __construct() • Constructor usually initializes class members, attributes
or has the other object • Example • Car has or owns Motor • When Car is build, it's motor is built also • When Car is destroyed it's motor is detroyed • Composition can be one to one or one to many
__destruct() { print "Motor is killed\n"; } } class Car { private $motor; function __construct() { print "Car!\n"; $this-‐>motor = new Motor(); } function __destruct() { print "Car is killed\n"; } } $c = new Car();
} function __destruct() { print "Professor is deleted\n"; } } class Department { private $professors; function __construct($professor) { $this-‐>professors = array(); $this-‐>professors[] = $professor; print "Deparment was created\n"; } function addProfessor($professor) { $this-‐>professors[] = $professor; } function __destruct() { print "Department is deleted\n"; } } $deartment = new Department(); $tina = new Professor(); $jack = new Professor(); $department-‐>addProfessor($tina); $department-‐>addProfessor($jack);
1.1; $boolean = true; $string = "moi"; // COMPOUND types class Person { } $array = [1,2,3,4]; $object = new Person(); // SPECIAL types $resource = // some link to external resource $null = NULL;
derived class inherites • methods • attributes • .. of pre-‐existing base classes • Intended to help reuse of existing code with little or no modification
class, which inherits another class and so on • Mammal <-‐ Human <-‐ Worker <-‐ Programmer • When changing the base class all the derived classes change also!
coffee"; } } class Programmer extends Human { public function codeApps() { $this->drinkCoffee(); // Can't access because private print "Code apps"; } } $jack = new Programmer(); $jack->drinkCoffee(); // Can't access because private $jack->codeApps();
class Programmer extends Person { } // Jack now does not have $name, it does not // know anything about it. $jack = new Programmer(); // We are adding a NEW property to programmer! // This can be very confusing! $jack->name = "Jack Smith"; print $jack->name; // Outputs "Jack Smith"
instantiate (create objects) • It's possible to inherite abstract class and create objects from the inherited class, it it is concrete one • Abstract class may have abstract methods that do not have implementations • Abstract methods must be implemented in derived classes
surfaceArea(); } class Circle extends Shape { public $radius; public function surfaceArea() { return pi() * $this->radius * $this->radius; } } class Rectangle extends Shape { public $width; public $height; public function surfaceArea() { return $this->width() * $this->height; } } $c = new Circle(); $c->x = 50; $c->y = 50; $c->radius = 3; print ($c->surfaceArea());
{ public function addMoreGasoline(); public function reduceGasoline(); } class Car implements MovingObject, RunsOnGasoline { public function move() { print "Car is moving"; } public function addMoreGasoline() { ... } public function reduceGasoline() { ... } } class Mammal {} class Human extends Mammal implements MovingObject { public function move() { print "Human is moving"; } }
• If not, error is generated • Valid types in PHP5 • class/interface, self, array, callable • Valid types in PHP7 • In addition to the previous, bool, float, int string
"$this->name is moving\n"; } } class Dog extends Mammal { public function bark() { print "$this->name is barking\n"; } } class Human extends Mammal { public function createArt() { print "$this->name is creating art\n"; } } $human = new Human(); $human->name = "Jack"; $dog = new Dog(); $dog->name = "Spot"; doSomething($human); doSomething($dog); function doSomething(Mammal $a) { $a->move(); } You can pass Mammals, Dogs and Humans!
} class Dog extends Mammal { public function makeSound() { print "$this->name is barking\n"; } } class Human extends Mammal { public function makeSound() { print "$this->name is creating älämölö\n"; } } $human = new Human(); $human->name = "Jack"; $dog = new Dog(); $dog->name = "Spot"; doSomething($human); doSomething($dog); function doSomething(Mammal $a) { $a->makeSound(); } Now every Mammal has makeSound method so this will always work!
MovingObject { public function move() { print "Car is moving"; } } abstract class Mammal { public $name; abstract public function makeSound(); } class Human extends Mammal implements MovingObject { public function makeSound() { print "$this->name is creating älämölö\n"; } public function move() { print "$this->name is moving"; } } $human = new Human(); $human->name = "Jack"; $car = new Car(); doSomething($human); doSomething($car); function doSomething(MovingObject $a) { $a->move(); } Now you can pass objects that implements MovingObject!
MovingObject { public function move() { print "Car is moving"; } } class Robot implements MovingObject { public function move() { print "Robot is moving"; } } class RemoteControl { private $target; public function __construct(MovingObject $target) { $this->target = $target; } public function keyArrowRight() { $this->target->move(); } } $r = new RemoteControl(new Car()); $r->keyArrowRight();