Download presentation
Presentation is loading. Please wait.
Published byElwin Conley Modified over 9 years ago
1
Object-Oriented PHP (3) Intermediate OO Concepts & Practice (2)
2
Correcting a dud example – the Student class refactored Student class: include_once('Person_Class.php'); class Student extends Person { private $programme; private $number; public function __construct($n, $d, $g, $p, $no) { parent::__construct($n, $d, $g); $this->programme = $p; $this->number = $no; } public function __get($var) { if (preg_match('/name|dob|gender/', $var)) { return parent::__get($var); } return $this->$var; } overrides parents __get (but a bit of bad design here)
3
Correcting a dud example – the Student class (2) refactored Student class: include_once('Person_Class.php'); class Student extends Person { private $programme; private $number; public function __construct($n, $d, $g, $p, $no) { parent::__construct($n, $d, $g); $this->programme = $p; $this->number = $no; } public function __get($var) { if (preg_match('/programme|number/', $var)) { return $this->$var; } else { return parent::__get($var); } corrected
4
Refactoring the Person class with magic __get & __set date_default_timezone_set('Europe/London'); class Person { private $name, $dob, $gender; function __construct($n, $d, $g) { $this->name = $n; $this->dob = $d; $this->gender = $g; } public function __get($property) { return $this->$property; } public function __set($property, $value) { $this->$property = $value; } public function get_age() { return floor((time() - strtotime($this->dob))/31556926); }
5
Testing the Person class include_once('Person_Class.php'); $me = new Person('Prakash', '1962-09-01', 'male'); #print details – uses the __get() method for name & gender echo $me->name.' is '.$me->gender.' and is '. $me->get_age.' '; #oops – told a lie – 2 years older – uses the __set() method $me->dob('1960-09-01'); #print details again echo $me->name.' is '.$me->gender.' and is '. $me->get_age.' '; test_Person_Class.php : run it
6
Abstract Methods & Classes OO programs are built around class hierarches PHP supports single inheritance – so class hierarchies can be visualised as trees a root class has one or more classes that descend from it, with one or more classes derived from them abstract methods are methods that behave like placeholders for regular methods in derived classes but unlike regular class methods they do not contain any code they bind any derived (extended) classes to a contract to implement the methods defined
7
Abstract Methods & Classes (continued) hence – abstract classes define common functionality the base classes must implement abstract classes are best thought of as a template for derived classes abstract classes cannot be directly instantiated i.e. you cannot create objects from them using the new keyword – they must be extended by concrete classes which can then be instantiated if a class contains an abstract method – it must be declared as abstract abstract classes can also contain concrete methods any method that is declared abstract, when implemented, must contain the same or weaker access level an abstract class can be extended without implementing all of its methods if the extended class is declared as abstract too – useful for creating hierarchies of objects
8
Abstract Shape class example (again) // Define abstract class with one abstract method abstract class Shape { public abstract function area(); }
9
class Circle extends Shape { private $radius; public function __construct($r) { $this->radius = $r; } public function area() { return $this->radius * $this->radius * pi(); } class Rectangle extends Shape { private $length; private $width; public function __construct($l, $w) { $this->length = $l; $this->width = $w; } public function area() { return $this->length * $this->width; } Two concrete classes that extend the Shape class must implement contract
10
$c = new Circle(22); echo "Area of the circle: ". $c->area(). " "; $r = new Rectangle(5, 7); echo "Area of the rectangle: ". $r->area(). " "; Instantiate two objects (Circle, Rectangle) and use their area() method run itview code
11
Abstract Car, FastCar & Street classes example the Car and FastCar classes: abstract class Car { abstract function getMaximumSpeed(); } class FastCar extends Car { function getMaximumSpeed() { return 150; }
12
Abstract Car, FastCar & Street classes example (2) the Street class: class Street { protected $speedLimit; protected $cars; public function __construct($speedLimit = 200) { $this->cars = array(); //Initialize the variable $this->speedLimit = $speedLimit; } protected function isStreetLegal($car) { if($car->getMaximumSpeed() speedLimit) { return true; } else { return false; } public function addCar($car) { if($this->isStreetLegal($car)) { echo 'The Car was allowed on the road.'; $this->cars[] = $car; } else { echo 'The Car is too fast and was not allowed on the road.'; }
13
Instantiate Street & FastCar objects and add car to cars[] array if legal (i.e. under 200) $street = new Street(); $street->addCar(new FastCar()); run itview code
14
Emulating multiple inheritance with interfaces whilst abstract classes can have concrete methods (i.e. provide a measure of implementation) - interfaces are pure templates; hence an interface can only define functionality but can never implement it; interfaces offer a way of inheriting from a single parent while sharing an extra set of common features that don’t necessarily apply to all child classes; any class that that incorporates an interface is committed to a contract to implement all the methods defined in the interface; all methods declared in an interface are public; it’s possible for interfaces to have constants but not instance variables. Interface constants work exactly like class constants except they cannot be overridden by a class/interface that inherits it;
15
Emulating multiple inheritance with interfaces (example) class Vehicle { private $name, $model; function __construct($make, $model) { $this->make = $make; $this->model = $model; } function __get($value) { return $this->$value; } interface TestDrive_Interface { function drive($to, $from); } class Bike extends Vehicle {} class Car extends Vehicle implements TestDrive_Interface { function drive($to='there', $from='here') { $this->to = $to; $this->from = $from; return "from $this->from to $this->to"; } $bike = new Bike('Raleigh', 'Chopper'); echo $bike->make.' '.$bike->model.' '; $car = new Car('Lotus', 'Esprit'); echo $car->drive('Swindon', 'Bristol'); run it
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.