UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
Object-Oriented PHP (3) Intermediate OO Concepts & Practice (2)
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Object-Oriented PHP (1)
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 SWE Introduction to Software Engineering Lecture 23 – Architectural Design (Chapter 13)
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
ASP.NET Programming with C# and SQL Server First Edition
BACS 287 Basics of Object-Oriented Programming 1.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Introduction to Object-oriented programming and software development Lecture 1.
Chapter 8 More Object Concepts
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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)
CSCI-383 Object-Oriented Programming & Design Lecture 13.
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.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Object-Oriented Programming Chapter Chapter
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Object Oriented Programming
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
ISBN Object-Oriented Programming Chapter Chapter
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,
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Object-Oriented PHP (2) Intermediate OO Concepts & Practice (1)
OOP Basics Classes & Methods (c) IDMS/SQL News
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Welcome to OBJECT ORIENTED PROGRAMMING Prepared By Prepared By : VINAY ALEXANDER PGT(CS) KV jhagrakhand.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Object-Oriented Programming Concepts
Inheritance ITI1121 Nour El Kadri.
CHAPTER 5 GENERAL OOP CONCEPTS.
Final and Abstract Classes
Review: Two Programming Paradigms
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Object Oriented Analysis and Design
Object-Oriented Programming
Object-Oriented PHP (1)
Final and Abstract Classes
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)

o simplicity: software objects model real world objects, so the complexity is reduced and the program structure is very clear; o modularity: each object forms a separate entity whose internal workings are decoupled from other parts of the system; o modifiability: it is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods; o extensibility: adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones; o maintainability: objects can be maintained separately, making locating and fixing problems easier; o re-usability: objects can be reused in different programs. strong cohesion loose coupling abstraction encapsulation the object-oriented approach

many modern programming languages either require (java, ruby, smalltalk etc.) or support (php, perl, F# etc.) the oo approach to software development. object-oriented development attempts to use the classifications, relationships, and properties of the “objects” in the system to aid in program development and code reuse. the oo approach can be compared and contrasted with other approaches to programming – e.g. imperative, procedural, functional, logical, event-driven, declarative, aspect-oriented ….

o in oo terminology, an “object” can represent something “real” (e.g. an user) or something “conceptual” (e.g. a file) o o oo software is designed and built as a set of self- contained objects that have both attributes (properties) and operations (behaviours) that interact with other objects (with attributes and behaviours) o a major advantage of o-o software is its capability to support and encourage encapsulation – or data hiding. Essentially, access to the data within an object is only available via an objects operations – known as the objects interface classes and objects (1)

classes and objects (2) o an objects functionality is bound to the data it uses o it is possible to alter the internals of an object to improve performance, add new features etc. without altering its interface (how it is accessed) o hence, an oo approach can help manage complexity; increase code reusability and thereby reduce maintenance costs o objects can be grouped into “classes” – a class can be thought of as a template for objects of that type

creating classes and class structure o structure of a class : a minimal class definition o class Classname { } o however, to be useful a class needs attributes (properties) and operations (methods) o attributes (properties) are created using keywords that match their visibility : public, private or protected o the following code creates a class with three public attributes class Person { public $name; public $dob; public $gender; }

o operations or methods are created by declaring functions within the class definition o the following code extends the Person class with two operations (methods) set_name and get_name o the first method takes 1 parameter and the second takes none but operations can take 0 to any number of parameters class Person { function set_name($name) { $this->name = $name; } function get_name(){ return $this->name; } class level operations and methods

o most classes have a special operation called a constructor - which is always called when an object of that class is created o a constructor is like other operations (methods) but has the special name __ construct() [note the double underscores __ ] o the following code re-defines the Person class with a constructor class Person { function __construct($n, $d, $g){ $this->name = $n; $this->dob = $d; $this->gender = $g; } constructors

instantiating classes o after a class has been declared - an object can be created of that class o such an object is an “instantiation” of that class (a particular individual that is a member of that class) o objects are created using the new keyword o when the object is created any parameters required by the __ construct method (operation) can be provided o - the following code uses our Person class passing it the expected parameters to creates three Person objects …

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; } # create two objects $p1 = new Person (‘Anna’, ‘11/10/83’, ‘woman’); $p2 = new Person (‘James’, ‘02/10/81’, ‘man’); # call the get_name() method echo $p1->get_name().’ ’; echo $p2->get_name(); instantiating the person class, creating two person objects & calling the get_name() method run script

controlling access with private, protected and public o php uses access modifiers to control the visibility of attributes and methods (functions) - these modifiers are placed in front of attributes and methods o 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 o 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 o the protected access modifier means that the marked item can only be accessed from inside the class but also exists in any inherited classes - protected is kind of half way between public and private

destructors o the opposite of a constructor is a destructor o this allows for some functionality to be executed just before an object is destroyed o this will happen automatically when all references to a class have been unset or fallen out of scope o a destructor for a class must be named _destruct() o a destructor cannot take parameters

inheritance o inheritance allows for the building of hierarchical relationships between classes using subclasses o a subclass inherits attributes and operations from its superclass o with inheritance it is possible to extend existing classes and thereby derive more complex and specialized classes o it also allows for common operations to be put once in the superclass rather than many times in separate subclasses

new class Student inheriting from the Person class class Student extends Person { public $programme; public $number; public function __construct($n, $d, $g, $p, $no) { parent::__construct($n, $d, $g); $this->programme = $p; $this->number = $no; } function get_programme(){ return $this->programme; } function get_number(){ return $this->number; } $s = new Student (‘Reuben’, ’25/08/93’, ‘male’, ‘Systems Analysis’, ‘ ’); echo $s->get_name(); echo $s->get_programme(); Run

abstract classes o abstract classes allow us to define base functionality that all child classes must implement o they can define concrete base functions as well as abstract functions that bind child classes to a contract o abstract classes cannot be instantiated – e.g. you cannot have a abstract class called Person and then instantiate a new Person object o one abstract class can be extended by another abstract class

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(); } abstract class example

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; } $c = new Circle(22); echo "Area of the circle: ". $c->area(). " "; $r = new Rectangle(5, 7); echo "Area of the rectangle: ". $r->area(). " "; abstract class example (cont.) Run

polymorphism o polymorphism is taken from the Greek and means ‘many forms’ o in oo programming it usually refers to a method (function) or operator that can have the same name but can behave differently in different contexts o for instance in the previous example the abstract class called ‘shape’ has a method called ‘area’ which is implemented in one way in the object called ‘circle’ and in another way in the object called ‘rectangle’

resources Introduction to PHP Objects, Part 1 Introduction to PHP Objects, Part 1 : John Coggeshall Introduction to PHP Objects, Part 2 Introduction to PHP Objects, Part 2 : John Coggeshall Getting started with objects with PHP V5 Getting started with objects with PHP V5 : Matt Zandstra