Object-Oriented PHP (Chapter 6).

Slides:



Advertisements
Similar presentations
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Advertisements

Object-Oriented PHP (1)
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Lecture 9 Concepts of Programming Languages
ASP.NET Programming with C# and SQL Server First Edition
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,
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
An Object-Oriented Approach to Programming Logic and Design
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.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
JavaScript, Fourth Edition
CSCI-383 Object-Oriented Programming & Design Lecture 13.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
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.
Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Basic OOP Concepts and Terms. In this class, we will cover: Objects and examples of different object types Classes and how they relate to objects Object.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Introduction to Object-Oriented Programming Lesson 2.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 10 Developing Object-Oriented PHP. 2 Objectives In this chapter, you will: Study object-oriented programming concepts Use objects in PHP scripts.
Object-Oriented Programming: Inheritance and Polymorphism.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Chapter 11 Developing Object-Oriented PHP PHP Programming with MySQL Revised by A. Philipp – Spring 2010 (Rev SP’11)
Can perform actions and provide communication
Chapter 9 Objects and Classes
Can perform actions and provide communication
Object-Oriented Programming: Inheritance and Polymorphism
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Object-Oriented PHP (1)
Presentation transcript:

Object-Oriented PHP (Chapter 6)

Developing Object-Oriented PHP Topics: OOP concepts – overview, throughout the chapter Defining and using objects Defining and instantiating classes Defining and using variables, constants, and operations Getters and setters Defining and using inheritance and polymorphism Building subclasses and overriding operations Using interfaces Advanced object-oriented functionality in PHP Comparing objects, Printing objects, Type hinting, Cloning objects, Overloading methods, (some sections WILL NOT BE COVERED!!!)

Object-Oriented Programming Object-oriented programming (OOP) refers to the creation of reusable software object-types / classes that can be efficiently developed and easily incorporated into multiple programs. In OOP an object represents an entity in the real world (a student, a desk, a button, a file, a text input area, a loan, a web page, a shopping cart). An OOP program = a collection of objects that interact to solve a task / problem.

Object-Oriented Programming Objects are self-contained, with data and operations that pertain to them assembled into a single entity. In procedural programming data and operations are separate → this methodology requires sending data to methods! Objects have: Identity; ex: 2 “OK” buttons, same attributes → separate handle vars State → a set of attributes (aka member variables, properties, data fields) = properties or variables that relate to / describe the object, with their current values. Behavior → a set of operations (aka methods) = actions or functions that the object can perform to modify itself – its state, or perform for some external effect / result.

Object-Oriented Programming Encapsulation (aka data hiding) central in OOP = access to data within an object is available only via the object’s operations (= known as the interface of the object) = internal aspects of objects are hidden, wrapped as a birthday present is wrapped by colorful paper  Advantages: objects can be used as black-boxes, if their interface is known; implementation of an interface can be changed without a cascading effect to other parts of the project → if the interface doesn’t change

Object-Oriented Programming Classes are constructs that define objects of the same type. A class is a template or blueprint that defines what an object’s data and methods will be. Objects of a class have: Same operations, behaving the same way Same attributes representing the same features, but values of those attributes (= state) can vary from object to object An object is an instance of a class. (terms objects and instances are used interchangeably) Any number of instances of a class can be created.

OOP in Web Programming Small Web projects Large Web software projects Consist of web scripts designed and written using an ad-hoc approach; a function-oriented, procedural methodology Large Web software projects Need a properly thought-out development methodology – OOP → OO approach can help manage project complexity, increase code reusability, reduce costs. OO analysis and design process = decide what object types, what hidden data/operations and wrapper operations for each object type UML – as tool in OO design, to allow to describe classes and class relationships

Creating Classes in PHP A minimal class definition: class classname { // classname is a PHP identifier! // the class body = data & function member definitions } Attributes are declared as variables within the class definition using keywords that match their visibility: public, private, or protected. (Recall that PHP doesn't otherwise have declarations of variables → data member declarations against the nature of PHP?) Operations are created by declaring functions within the class definition. Public can be replaced with var -> public visibility by default!

Creating Classes in PHP Constructor = function used to create an object of the class Declared as a function with a special name: function __construct (param_list) { … } Usually performs initialization tasks: e.g. sets attributes to appropriate starting values Called automatically when an object is created A default no-argument constructor is provided by the compiler only if a constructor function is not explicitly declared in the class Cannot be overloaded (= 2+ constructors for a class); if you need a variable # of parameters, use flexible parameter lists…

Creating Classes in PHP Destructor = opposite of constructor Declared as a function with a special name, cannot take parameters function __destruct () { … } Allows some functionality that will be automatically executed just before an object is destroyed An object is removed when there is no reference variable/handle left to it Usually during the "script shutdown phase", which is typically right before the execution of the PHP script finishes A default destructor provided by the compiler only if a destructor function is not explicitly declared in the class If ref var is assigned null -> destructor called if no other ref var left to that object!

Instantiating Classes Create an object of a class = a particular individual that is a member of the class by using the new keyword: $newClassVariable = new ClassName(actual_param_list); Notes: Scope for PHP classes is global (program script level), as it is for functions Class names are case insensitive as are functions PHP 5 allows you to define multiple classes in a single program script The PHP parser reads classes into memory immediately after functions  class construction does not fail because a class is not previously defined in the program scope.

Using Data/Method Members From operations within the class, class’s data / methods can be accessed / called by using: $this = a variable that refers to the current instance of the class, and can be used only in the definition of the class, including the constructor & destructor The pointer operator -> (similar to Java’s object member access operator “.” ) class Test { public $attribute; function f ($val) { $this -> attribute = $val; // $this is mandatory! } // if omitted, $attribute is treated } // as a local var in the function No $ sign here

Using Data/Method Members From outside the class, accessible (as determined by access modifiers) data and methods are accessed through a variable holding an instance of the class, by using the same pointer operator. class Test { public $attribute; } $t = new Test(); $t->attribute = “value”; echo $t->attribute;

BankAccount.php http://www.nku.edu/~frank/csc301/Examples/PHP_OO/BankAccount.php http://www.nku.edu/~frank/csc301/Examples/PHP_OO/BankAccount_php.pdf

Defining and Using Variables, Constants and Functions Three access / visibility modifiers introduced in PHP 5, which affect the scope of access to class variables and functions: public : public class variables and functions can be accessed from inside and outside the class protected : hides a variable or function from direct external class access + protected members are available in subclasses private : hides a variable or function from direct external class access + protected members are hidden (NOT available) from all subclasses An access modifier has to be provided for each class instance variable Static class variables and functions can be declared without an access modifier → default is public

Getters and Setters Encapsulation : hide attributes from direct access from outside a class and provide controlled access through accessor and mutator functions You can write custom getVariable() / setVariable($var) functions or Overload the functionality with the __get() and __set() functions in PHP __get() and __set() Prototype: mixed __get($var); // param represents the name of an attribute, __get returns the value of that attribute void __set($var, $value); // params are the name of an attribute and the value to set it to

Getters and Setters __get() and __set() Can only be used for non-static attributes! You do not directly call these functions; For an instance $acc of the BankAccount class: $acc->Balance = 1000; implicitly calls the __set() function with the value of $name set to ‘Balance’, and the value of $value set to 1000. (__get() works in a similar way)

GetAndSet.php http://www.nku.edu/~frank/csc301/Examples/PHP_OO/GetAndSet.php http://www.nku.edu/~frank/csc301/Examples/PHP_OO/GetAndSet_php.pdf

Getters and Setters __get() and __set() functions’ value: a single access point to an attribute ensures complete control over: attribute’s values function __set($name, $value) { echo "<p>Setter for $name called!</p>"; if (strcasecmp($name, "Balance")==0 && ($value>=0)) $this->$name = $value; ... } underlying implementation: as a variable, retrieved from a db when needed, a value inferred based on the values of other attributes → transparent for clients as long as the accessor / mutator functions’ contract doesn’t change.

Designing Classes Classes in Web development: Pages User-interface components Shopping carts Product categories Customers TLA Consulting example revisited - a Page class, goals: A consistent look and feel across the pages of the website Limit the amount of HTML needed to create a new page: easily generate common parts, describe only uncommon parts Easy maintainable when changes in the common parts Flexible enough: ex. allow proper navigation elements in each page

TLA Consulting http://www.nku.edu/~frank/csc301/Examples/PHP_OO/home.php http://www.nku.edu/~frank/csc301/Examples/PHP_OO/page_php.pdf http://www.nku.edu/~frank/csc301/Examples/PHP_OO/home_php.pdf

Class Page Attributes: Operations: $content → content of the page, a combination of HTML and text $title → page’s title, with a default title to avoid blank titles $keywords → a list of keywords, to be used by search engines $navigation → an associative array with keys the text for the buttons and the value the URL of the target page Operations: __set() Display() → to display a page of HTML, calls other functions to display parts of the page: DisplayTitle(), DisplayKeywords(), DisplayStyles(), DisplayHeader(), DisplayMenu(), DisplayFooter() → can be overridden in a possible subclass