2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation.

Slides:



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

Final and Abstract Classes
Pemrograman Web Object Oriented Programming in PHP 5.
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Object Oriented Programming in PHP 5
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Object-Oriented PHP (1)
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
Classes and Objects in Java
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
OOP! POO! Spelled backwards. Intro to OOP What is OOP?  Stands for Object Oriented Programming  Create different types of objects which can do multiple.
ASP.NET Programming with C# and SQL Server First Edition
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
10-Aug-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is an abstract description of objects.
PHP Workshop ‹#› PHP Classes and Object Orientation.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Functions & Objects IDIA 618 Spring 2012 Bridget M. Blodgett.
Chapter 8 More Object Concepts
CMP-MX21: Lecture 6 Objects & Methods 1 Steve Hordley.
Objectives In this chapter, you will:
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
ClassesPHPMay-2007 : [‹#›] PHP Classes and Object Orientation.
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.
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 07.
Working With Objects Tonga Institute of Higher Education.
Classes and Objects in Java
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
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.
Coming up: Inheritance
TeachJava! 2003 Corky Cartwright Dung Nguyen Stephen Wong Charlie Reis, James Hsia, Peter Centgraf.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Classes in PHP Web Engineering. What is Class? A class is a collection of variables and functions working with these variables. Variables are defined.
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.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
 We have created our two separate 'person' objects, we can set their properties using the methods (the setters) we created.  class person {  var $name;
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
Week 5 Extending classes in Java. Extending classes u Using an existing classes as the basis for a new one u Defining only the differences between the.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Seventh step for Learning C++ Programming CLASS Declaration Public & Private Constructor & Destructor This pointer Inheritance.
The Object-Oriented Thought Process Chapter 03
University of Central Florida COP 3330 Object Oriented Programming
Classes & Objects There are two main programming paradigms: Procedural Object-Oriented Up to now, everything we have done has been procedural.
Polymorphism 2nd Lecture
Classes and Objects in Java
University of Central Florida COP 3330 Object Oriented Programming
Chapter 11 Developing Object-Oriented PHP PHP Programming with MySQL Revised by A. Philipp – Spring 2010 (Rev SP’11)
Web Systems Development (CSC-215)
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
PHP Classes and Object Orientation
Polymorphism 2nd Lecture
Interface.
An Introduction to Object Orientated Programming
Classes and Objects in Java
Object-Oriented Programming in PHP
CLASSES, AND OBJECTS A FIRST LOOK
Object-Oriented Programming
Review of Previous Lesson
Object-Oriented PHP (1)
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Classes and Objects in Java
Presentation transcript:

2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [2]Building Web Applications using MySQL and PHP (W1)OO PHP Reminder... a function Reusable piece of code Has its own ‘local scope’. function my_func($arg1, arg2) { //function statements }

2010/11 : [3]Building Web Applications using MySQL and PHP (W1)OO PHP...give the function something (arguments), it does something with them, and then returns a result... Action or Method Conceptually, what does a function represent?

2010/11 : [4]Building Web Applications using MySQL and PHP (W1)OO PHP What is a class? Conceptually, a class represents an object, with associated methods and variables

2010/11 : [5]Building Web Applications using MySQL and PHP (W1)OO PHP <?php class Dog { public $name; function bark() { echo 'Woof'; } ?> Class Definition An example class definition for a dog. Every dog object has a single attribute, the name, and can perform the action of barking.

2010/11 : [6]Building Web Applications using MySQL and PHP (W1)OO PHP <?php class dog { public $name; function bark() { echo 'Woof'; } ?> Class Definition Define the name of the class.

2010/11 : [7]Building Web Applications using MySQL and PHP (W1)OO PHP <?php class dog { public $name; function bark() { echo 'Woof'; } ?> Class Definition Define an object attribute (variable) the dog’s name.

2010/11 : [8]Building Web Applications using MySQL and PHP (W1)OO PHP <?php class dog { public $name; function bark() { echo 'Woof'; } ?> Class Definition Define an object action (function), the dog’s bark.

2010/11 : [9]Building Web Applications using MySQL and PHP (W1)OO PHP <?php class dog { public $name; function bark() { echo 'Woof'; } ?> Class Definition End the class definition

2010/11 : [10]Building Web Applications using MySQL and PHP (W1)OO PHP Similar to defining a function... The definition does not do anything by itself. It is a blueprint, or description, of an object. To do something, you need to use the class... Class definition

2010/11 : [11]Building Web Applications using MySQL and PHP (W1)OO PHP <?php require('dog.class.php'); $puppy = new dog(); $puppy->name='Rover'; echo $puppy->name.' says'; $puppy->bark(); ?> Class usage

2010/11 : [12]Building Web Applications using MySQL and PHP (W1)OO PHP <?php require('dog.class.php'); $puppy = new dog(); $puppy->name='Rover'; echo $puppy->name.' says'; $puppy->bark(); ?> Class usage Include the class definition

2010/11 : [13]Building Web Applications using MySQL and PHP (W1)OO PHP <?php require('dog.class.php'); $puppy = new dog(); $puppy->name='Rover'; echo $puppy->name.' says'; $puppy->bark(); ?> Class usage Create a new instance (object) of the class

2010/11 : [14]Building Web Applications using MySQL and PHP (W1)OO PHP <?php require('dog.class.php'); $puppy = new dog(); $puppy->name='Rover'; echo $puppy->name.' says'; $puppy->bark(); ?> Class usage Set the name variable of this instance to ‘Rover'.

2010/11 : [15]Building Web Applications using MySQL and PHP (W1)OO PHP <?php require('dog.class.php'); $puppy = new dog(); $puppy->name='Rover'; echo $puppy->name.' says'; $puppy->bark(); ?> Class usage Use the name attribute of this instance in an echo statement.

2010/11 : [16]Building Web Applications using MySQL and PHP (W1)OO PHP <?php require('dog.class.php'); $puppy = new dog(); $puppy->name='Rover'; echo $puppy->name.' says'; $puppy->bark(); ?> Class usage Use the object method bark().

2010/11 : [17]Building Web Applications using MySQL and PHP (W1)OO PHP $puppy->name = 'Rover'; The most common mistake is to use more than one dollar sign when accessing variables. The following means something entirely different. $puppy->$name = 'Rover'; One dollar and one only...

2010/11 : [18]Building Web Applications using MySQL and PHP (W1)OO PHP If you need to use the class variables within any class methods, use the special variable $this in the definition: class Dog { public $name; function bark() { echo $this->name.' says Woof!'; } Using attributes within the class

2010/11 : [19]Building Web Applications using MySQL and PHP (W1)OO PHP A constructor method is a function that is automatically executed when the class is first instantiated. Create a constructor by including a function within the class definition with the name __construct Remember, if the constructor requires arguments, they must be passed when it is instantiated! Constructor methods

2010/11 : [20]Building Web Applications using MySQL and PHP (W1)OO PHP <?php class Dog { var $name; function __construct($nametext) { $this->name=$nametext; } function bark(){echo 'Woof!';} } ?> Constructor example

2010/11 : [21]Building Web Applications using MySQL and PHP (W1)OO PHP <?php //... $puppy = new Dog('Rover'); //... ?> [example file: classes2.php] Constructor example Constructor arguments are passed during the instantiation of the object.

2010/11 : [22]Building Web Applications using MySQL and PHP (W1)OO PHP Like functions, each instantiated object has its own local scope. e.g. If 2 different dog objects are instantiated, $puppy1 and $puppy2, the two dog names $puppy1->name and $puppy2->name are entirely independent. Class scope $puppy1:Dog name Jack $puppy2:Dog name Lassie

2010/11 : [23]Building Web Applications using MySQL and PHP (W1)OO PHP HOE: Using Classes

2010/11 : [24]Building Web Applications using MySQL and PHP (W1)OO PHP The real power of using classes is the property of inheritance – creating a hierarchy of interlinked classes. Inheritance Dog PoodleAlsatian parent children

2010/11 : [25]Building Web Applications using MySQL and PHP (W1)OO PHP The child classes ‘inherit’ all the methods and variables of the parent class, and can add extra ones of their own. e.g. The child class poodle inherits the variable ‘name’ and method ‘bark’ from the Dog class, and can add extra ones... Inheritance

2010/11 : [26]Building Web Applications using MySQL and PHP (W1)OO PHP The American Kennel Club (AKC) recognises three sizes of poodle – Standard, Miniature and Toy... class Poodle extends Dog { public $type; function set_type($height) { if ($height<10) { $this->type = 'Toy'; } elseif ($height>15) { $this->type = 'Standard'; } else { $this->type = 'Miniature'; } Inheritance example Note the use of the extends keyword to indicate that the Poodle class is a child of the Dog class...

2010/11 : [27]Building Web Applications using MySQL and PHP (W1)OO PHP... $puppy = new Poodle('Oscar'); $puppy-set_type(12);//12 inches high! echo 'Poodle is called '.$puppy->name; echo ", of type {$puppy->type}, saying "; echo $puppy->bark();... [example file: classes3.php] Inheritance example

2010/11 : [28]Building Web Applications using MySQL and PHP (W1)OO PHP It is possible to over-ride a parent method with a new method if it is given the same name in the child class class Poodle extends Dog { //... function bark(){ echo 'Yip!'; } //... } [example file: classes4.php]...a poodle will always 'Yip!'

2010/11 : [29]Building Web Applications using MySQL and PHP (W1)OO PHP If the child class possesses a constructor function, it is executed and any parent constructor is ignored. If the child class does not have a constructor, the parent's constructor is executed. If the child and parent do not have a constructor, the grandparent constructor is attempted…etc… Child constructors?

2010/11 : [30]Building Web Applications using MySQL and PHP (W1)OO PHP It is perfectly possible to include objects within another object... class Dogtag { public $words; } class Dog { public $name; public $tag; function bark() { echo "Woof!\n";} } Objects within Objects... $puppy = new Dog(); $puppy->name = "Rover"; $puppy->tag = new Dogtag(); $puppy->tag->words = "blah";...

2010/11 : [31]Building Web Applications using MySQL and PHP (W1)OO PHP So far our objects have not been destroyed till the end of our scripts... Like variables, it is possible to explicitly destroy an object using the unset () function. Deleting objects

2010/11 : [32]Building Web Applications using MySQL and PHP (W1)OO PHP Entire objects can be passed as arguments to functions, and can use all methods/variables within the function. However, unlike normal function arguments the object is not copied when passed as an argument. A copy, or not a copy

2010/11 : [33]Building Web Applications using MySQL and PHP (W1)OO PHP HOE: Class Inheritance

2010/11 : [34]Building Web Applications using MySQL and PHP (W1)OO PHP Reason 1 Once you have your head round the concept of objects, intuitively named object orientated code becomes easy to understand. e.g. $order->display_basket(); $user->card[2]->pay($order); $order->display_status(); Why Object Orientate?

2010/11 : [35]Building Web Applications using MySQL and PHP (W1)OO PHP Reason 2 Existing code becomes easier to maintain. e.g. If you want to extend the capability of a piece of code, you can merely edit the class definitions... Why Object Orientate?

2010/11 : [36]Building Web Applications using MySQL and PHP (W1)OO PHP Reason 3 New code becomes much quicker to write once you have a suitable class library. e.g. Need a new object? Usually can extend an existing object. A lot of high quality code is distributed as classes (e.g. Why Object Orientate?

2010/11 : [37]Building Web Applications using MySQL and PHP (W1)OO PHP We have really only touched the edge of object orientated programming... I don't want to confuse you too much! There is a lot more