UFCEWT-20-3 : Advanced Topics in Web Development Lecture 7 : Object Oriented PHP (2)

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming with Java
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Object-Oriented PHP (3) Intermediate OO Concepts & Practice (2)
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 10: Introduction to Inheritance
Object-Oriented PHP (1)
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 10 Classes Continued
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Lecture 3 Casting Abstract Classes and Methods Interfaces.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Martin Kruliš This is an Object Oriented system. If we change something, the users object by Martin Kruliš (v1.0)1.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Inheritance in the Java programming language J. W. Rider.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/23 Outline Creating Subclasses Overriding Methods Class Hierarchies.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Abstract Classes. Review PA – 3 Design Considerations /Web/CS239/programs/pa3/draft.php ?harrisnlCS239
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance and Access Control CS 162 (Summer 2009)
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Intermediate PHP (4) Object-Oriented PHP (2). Object-oriented concepts Classes, attributes and operations Class attributes Per-class constants Class method.
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
Variations on Inheritance Object-Oriented Programming Spring
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Object-Oriented PHP (2) Intermediate OO Concepts & Practice (1)
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
1 More About Derived Classes and Inheritance Chapter 9.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Inheritance ITI1121 Nour El Kadri.
Final and Abstract Classes
Inheritance and Polymorphism
Advanced Programming in Java
Object Oriented Programming
CS240: Advanced Programming Concepts
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Lecture 14- Abstract Classes
Advanced Java Programming
Object-Oriented Programming in PHP
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
Object-Oriented PHP (1)
Chapter 8 Inheritance Part 2.
Final and Abstract Classes
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
Presentation transcript:

UFCEWT-20-3 : Advanced Topics in Web Development Lecture 7 : Object Oriented PHP (2)

Refactoring the Person/Student classes (1) : the Person Class (from last week) Person class: class Person { public $name; public $dob; public $gender; function __construct($n, $d, $g){ $this->name = $n; $this->dob = $d; $this->gender = $g; } function get_name(){ return $this->name; } public by default

(2) : the Student subclass class Student extends Person { public $programme; public $number; function __construct($n, $d, $g, $p, $no) { parent::__construct($n, $d, $g); $this->programme = $p; $this->number = $no; } (from last week) Student class: inheritance or sub-classing call parent method

Note on UML 2 notation: + public # protected – private // instantiate a student object $s $s = new Student ('Reuben', '1993/10/25', 'male', 'Systems Analysis', ' '); // invoke the get_name() method echo $s->get_name(); //filters up to parent Output: Reuben (3) : instantiate a Student object and call the get_name() method

(4) : add new get_age() method refactored Person class: 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($var) { return $this->$var; } public function get_age() { return floor((time() - strtotime($this->dob))/ ); } calculates current age

(5) : using the “magic” __get() method 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

public function __get($var) { if (preg_match('/name|dob|gender/', $var)) { return parent::__get($var); } return $this->$var; } !! Code smell alert : principle of loose coupling broken Why?

Correcting a dud example – the 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

(6) : the Lecturer class Lecturer class: include_once('Person_Class.php'); class Lecturer extends Person { private $modules=array(); private $room; function __construct($n, $d, $g, $m, $r) { parent::__construct($n, $d, $g); $this->modules = $m; $this->room = $r; } public function __get($var) { if (preg_match('/moduules|room/', $var)) { return $this->$var; } else { return parent::__get($var); }

<?php include('Student_Class.php'); include('Lecturer_Class.php'); // instantiate a student object $s $s = new Student ('Reuben', '1993/07/25','male','Systems Analysis', ' '); //invoke the __get() method echo $s->name; echo ' is doing '.$s->programme; echo ' and is '.$s->get_age().' years old '; // instantiate a new Lecturer $l = new Lecturer('Prakash', '1960/09/01','man', array('Web Programming','ISD','PEPI'), '3P16'); // echo name and each array element followed by an & (unless last element) echo $l->name.' teaches '; foreach($l->modules as $module) { echo $module; if (!(end($l->modules)==$module)) {echo ' & ';} } ?> (7) : Testing the Student & Lecturer classes run script

Controlling access with private, protected and public -php uses access modifiers to control the visibility of attributes and methods (functions) - these modifiers are placed in front of attributes and methods -the default option is public - that is, if no modifier is stated - it is assumed to be public - these can be accessed from inside or outside the class -the private access modifier can only be accessed from inside the class - if, for instance a method is a utility function and only to be used inside the class - private attributes & methods cannot be inherited -the protected access modifier means that the marked item can be accessed from inside the class but also exists in any inherited classes - protected is kind of half way between public and private

class methods, variables & constants o class methods are not run on specific a object instance – they have class only scope o from outside they have to be called using the class name o the keyword for class methods and variables is static o inside the class the qualifier self:: is used; o outside the class the qualifier Classname:: is used example static variable & method: class StaticExample { static public $aNum = 0; static public function sayHello() { echo 'hello'; } // from inside the class echo self::$aNum; self::sayHello(); } // from outside echo StaticExample::$aNum; StaticExample::sayHello();

class method & variable example (2): static variable class StaticExample { static public $aNum = 0; static public function sayHello() { self::$aNum++; echo 'hello ('.self::$aNum.') '; } test_static.php include ('StaticExample.php'); StaticExample::sayHello(); Output? run scriptrun script no need to instantiate the class

class method & constant class constants : o cannot be changed (hence ‘constant’) o they are always public o some restrictions on what they can hold (no objects for instance) o declared using the const keyword o not allowed to be defined inside methods example : class HowLong { // speed of light (meters per second) const C = ; // average distance from the earth to the sun (meters) const D = ; // method to calculate how long light takes to // get from the sun to the earth in minutes function minToEarth() { echo (self::D/self::C)/60; } HowLong::minToEarth(); run script

“final” classes and methods o inheritance is powerful and flexible allowing for sub-classing (specialisation) or overriding methods so that call to a client method will achive radically different results (see our Shape class example from last week) o sometimes though, we require a class or method to remain unchanging – to stop inheritance or overriding o The keyword final puts a stop to inheritance and does not allow methods to be overridden (i.e. you can instantiate a class with a final method but you can’t override that method) example: final class Checkout {}; // try to inherit class IllegalCheckout extends Checkout { }; Output: Fatal error: Class IllegalCheckout may not inherit from final class (Checkout) in C:\xampp\htdocs\php\illegal.php on line 5

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

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

Abstract Shape class example (again) // Define abstract class with one abstract method abstract class Shape { public abstract function area(); }

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

$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 script view code

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; }

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.'; }

Instantiate Street & FastCar objects and add car to cars[] array if legal (i.e. under 200) $street = new Street(); $street->addCar(new FastCar()); run scriptview code

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;

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 script

autoloading classes in php4 classes had to loaded with the include or require keywords in php5 classes can be loaded dynamically and on a need to use basis by using a “magic” method (or interceptor function) called __autoload example: function __autoload($className) { include_once($className.'_Class.php'); } //we have a class called Person_Class.php hence $person = new Person('Reuben', '1993/07/25','male'); // will work; hence echo $person->name; //is fine run script