Object-Oriented PHP (2) Intermediate OO Concepts & Practice (1)

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Final and Abstract Classes
Python Objects and Classes
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Object-Oriented PHP (3) Intermediate OO Concepts & Practice (2)
Inheritance Inheritance Reserved word protected Reserved word super
PACS – 10/19/131 Object-Oriented Programming “Object-oriented programming opens the door to cleaner designs, easier maintenance, and greater code reuseability.”
Object-Oriented PHP (1)
Road Map Introduction to object oriented programming. Classes
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Chapter 8 More Object Concepts
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Martin Kruliš This is an Object Oriented system. If we change something, the users object by Martin Kruliš (v1.0)1.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
UFCEWT-20-3 : Advanced Topics in Web Development Lecture 7 : Object Oriented PHP (2)
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Object-Oriented PHP (Chapter 6).
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/23 Outline Creating Subclasses Overriding Methods Class Hierarchies.
Abstract Classes. Review PA – 3 Design Considerations /Web/CS239/programs/pa3/draft.php ?harrisnlCS239
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
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.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
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.
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.
OOP Basics Classes & Methods (c) IDMS/SQL News
Object-Oriented Programming: Classes and Objects Chapter 1 1.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
UNIT-IV WT. Why use classes and objects? PHP is a primarily procedural language small programs are easily written without adding any classes or objects.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
OOP: Encapsulation &Abstraction
Examples of Classes & Objects
Final and Abstract Classes
Inheritance and Polymorphism
Elements of the PHP Programming Environment
Lecture 14 - Abstract Classes
CS240: Advanced Programming Concepts
Abstract Classes.
Classes & Objects: Examples
Lecture 14- Abstract Classes
Advanced Java Programming
Object-Oriented Programming in PHP
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Software Engineering for Internet Applications
Object Oriented Programming
Object-Oriented PHP (1)
Final and Abstract Classes
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

Object-Oriented PHP (2) Intermediate OO Concepts & Practice (1)

Refactoring the Person/Student class (1) (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

Refactoring the Person/Student class (2) 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 Refactoring the Person/Student classes (3)

Refactoring the Person/Student/Lecturer classes (4) 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

Refactoring the Person/Student/Lecturer classes (5) 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

Refactoring the Person/Student/Lecturer classes (6) 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("/name|dob|gender/", $var)) { return parent::__get($var); } return $this->$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 ' & ';} } ?> Refactoring the Person/Student/Lecturer classes (7) run it

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 class methods are not run on specific a object instance – they have class only scope from outside they have to be called using the class name the keyword for class methods and variables is static inside the class the qualifier self:: is used; 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 itrun it no need to instantiate the class

class method & constant class constants : cannot be changed (hence ‘constant’) they are always public some restrictions on what they can hold (no objects for instance) declared using the const keyword 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 it

“final” classes and methods 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) sometimes though, we require a class or method to remain unchanging – to stop inheritance or overriding 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

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 it more on “magic” methods next week ….