Object-Oriented PHP (1)

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Python Objects and Classes
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.
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 Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
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.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Programming Languages and Paradigms Object-Oriented Programming.
Introduction to Object-oriented programming and software development Lecture 1.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
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.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
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.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
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”
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object-Oriented Programming Chapter Chapter
M1G Introduction to Programming 2 5. Completing the program.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
ISBN Object-Oriented Programming Chapter Chapter
OOP Review CS 124.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Intermediate PHP (4) Object-Oriented PHP (2). Object-oriented concepts Classes, attributes and operations Class attributes Per-class constants Class method.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
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.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
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.
ISBN Chapter 12 Support for Object-Oriented Programming.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Final and Abstract Classes
Inheritance and Polymorphism
Object Oriented Analysis and Design
Object-Oriented Programming
Object-Oriented Programming
Object-Oriented Programming
Object-Oriented PHP (1)
Final and Abstract Classes
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Computer Science II for Majors
Presentation transcript:

Object-Oriented PHP (1)

Key topics: Object-oriented concepts Classes, attributes and operations Class attributes Per-class constants Class method invocation Inheritance Access modifiers Static methods Type hinting Late static bindings Object cloning Abstract classes Class design Implementing design Advanced OO functionality

- understanding Object-oriented concepts many modern programming languages either require (java, ruby, smalltalk etc.) or support (php, perl, F# etc.) the o-o 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.

- classes and objects (1) in o-o terminology, an “object” can represent something “real” (e.g. an user) or something “conceptual” (e.g. a file) o-o 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) - 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 (2) an objects functionality is bound to the data it uses it is possible alter the internals of an object to improve performance, add new features etc. without altering its interface hence, an o-o approach can help manage complexity; increase code reusability and thereby reduce maintenance costs objects can be grouped into “classes” – a class can be thought of as a template for objects of that type

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

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

- creating classes, attributes & operations in PHP structure of a class a minimal class definition class classname { } however, to be useful a class needs attributes and operations attributes are created using keywords that match their visibility : public, private or protected the following code creates a class with two public attributes class person { public $name; public $age; }

operations or methods are created by declaring functions within the class definition the following code creates a class named classname that do nothing the first operation op1() takes no parameters and the second op2() takes two parameters class classname { function op1() { } function op2 ($param1, $param2){

- constructors most classes have a special operation called a constructor - which is always called when an object of that class is created a constructor is like other operations (methods) but has the special name _construct() the following code defines a class with a constructor class classname { function _construct($param) { echo “Constructor called with parameter ”.$param.“<br/>”; }

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

- instantiating classes after a class has been declared - an object can be created of that class such an object is an “instantiation” of that class (a particular individual that is a member of that class) objects are created using the new keyword when the object is created any parameters required by the _construct method (operation) can be provided - the following code creates a class called classname with a constructor that expects one parameter - then three objects of that class are created

class classname { function _construct($param) { echo “conctruct called with”.$param.“<br/>”; } $a = new classname(“First”); $b = new classname(“Second”); $c = new classname(); what will be the output?

- using class attributes (1) within a class, attributes can be accessed using the special pointer $this for instance an attribute called $attribute can be accessed with $this—>attribute the following code illustrates setting and accessing an attribute within a class class classname { public $attribute; function operation($param) { $this—>attribute = $param; echo $this—>attribute; }

- using class attributes (2) weather an attribute can be accessed from outside a class is determined by access modifiers however, it is generally not recommended that attributes are accessed directly from outside the class - this is because the o-o approach encourages data hiding or encapsulation this is done using the __get & __set access functions; e.g. class classname { public $attribute; function __get($name) { return $this—>name; } function __set($name, $value) { $this—>name = value;

- using class attributes (3) the previous code example shows two interfaces to the $attribute attribute the __get() function simple returns the value of $attribute and __set() function assigns a new value to $attribute note that __get() takes one parameter - the name of an attribute and the __set() function takes two parameters - the name of an attribute and the value it is to be set to also note the use of the double underscore as in the __construct() and __destruct() functions php gives these functions special meaning and the __get() and __set() functions can be now be used to get and set the value of any attribute, e.g. $a = new classname; $a—>attribute = 5; // this will implicitly call the // set function on the attribute // called $attribute

- why bother? with a single access point (interface) it is now easy to implement validity checks for instance, if we now wanted to ensure that $attribute always has to be between 1 and 100 - we only need to modify the code in one place function __set($name, $value) { if (($name==“attribute” && ($value >= 1) && ($value <= 100) $this—>attribute = $value; }

- controlling access with private 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 only be accessed from inside the class but also exists in any inherited classes - protected is kind of half way between public and private