PHP Workshop ‹#› 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.
Object Oriented Programming in PHP 5
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.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
 Stands for "Object-Oriented Programming." OOP refers to a programming methodology based on objects, instead of just functions and procedures. These objects.
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.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance (notes for 10/26 lecture). Inheritance Inheritance is the last of the relationships we will study this semester. Inheritance is (syntactically)
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
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
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.
Functions & Objects IDIA 618 Spring 2012 Bridget M. Blodgett.
Chapter 8 More Object Concepts
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
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.
Computers and Scientific Thinking David Reed, Creighton University Functions and Libraries 1.
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.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation.
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.
Object Oriented Programming
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
TeachJava! 2003 Corky Cartwright Dung Nguyen Stephen Wong Charlie Reis, James Hsia, Peter Centgraf.
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.
Learning OOP in PHP. What is OOP? OOP stands for Object Oriented Programming. OOP is a programming paradigm wherein you create “objects” to work with.
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;
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
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
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
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.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
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.
Microsoft Visual Basic 2005: Reloaded Second Edition
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
An Introduction to Object Orientated Programming
Object-Oriented Programming in PHP
C++ Programming ㅎㅎ String OOP Class Constructor & Destructor.
CLASSES, AND OBJECTS A FIRST LOOK
Object-Oriented Programming
Chapter 9 Inheritance.
Review of Previous Lesson
Review of Previous Lesson
Object-Oriented PHP (1)
Final and Abstract Classes
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
C++ Object Oriented 1.
Presentation transcript:

PHP Workshop ‹#› PHP Classes and Object Orientation

PHP Workshop ‹#› Reminder… a function Reusable piece of code. Has its own ‘local scope’. function my_func($arg1,$arg2) { > }

PHP Workshop ‹#› Conceptually, what does a function represent? …give the function something (arguments), it does something with them, and then returns a result… Action or Method

PHP Workshop ‹#› What is a class? Conceptually, a class represents an object, with associated methods and variables

PHP Workshop ‹#› Class Definition <?php class dog { public $name; public function bark() { echo ‘Woof!’; } } ?> An example class definition for a dog. The dog object has a single attribute, the name, and can perform the action of barking.

PHP Workshop ‹#› Class Definition <?php class dog { public $name; public function bark() { echo ‘Woof!’; } } ?> class dog { Define the name of the class.

PHP Workshop ‹#› Class Definition <?php class dog { var $name public function bark() { echo ‘Woof!’; } } ?> public $name; Define an object attribute (variable), the dog’s name.

PHP Workshop ‹#› Class Definition <?php class dog { public $name; function bark() { echo ‘Woof!’; } } ?> public function bark() { echo ‘Woof!’; } Define an object action (function), the dog’s bark.

PHP Workshop ‹#› Class Definition <?php class dog { public $name; public function bark() { echo ‘Woof!’; } } ?> } End the class definition

PHP Workshop ‹#› Class Defintion 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…

PHP Workshop ‹#› Class Usage <?php require(‘dog.class.php’); $puppy = new dog(); $puppy->name = ‘Rover’; echo “{$puppy->name} says ”; $puppy->bark(); ?>

PHP Workshop ‹#› Class Usage <?php require(‘dog.class.php’); $puppy = new dog(); $puppy->name = ‘Rover’; echo “{$puppy->name} says ”; $puppy->bark(); ?> require(‘dog.class.php’); Include the class definition

PHP Workshop ‹#› Class Usage <?php require(‘dog.class.php’); $puppy = new dog(); $puppy->name = ‘Rover’; echo “{$puppy->name} says ”; $puppy->bark(); ?> $puppy = new dog(); Create a new instance of the class.

PHP Workshop ‹#› Class Usage <?php require(‘dog.class.php’); $puppy = new dog(); $puppy->name = ‘Rover’; echo “{$puppy->name} says ”; $puppy->bark(); ?> $puppy->name = ‘Rover’; Set the name variable of this instance to ‘Rover’.

PHP Workshop ‹#› Class Usage <?php require(‘dog.class.php’); $puppy = new dog(); $puppy->name = ‘Rover’; echo “{$puppy->name} says ”; $puppy->bark(); ?> echo “{$puppy->name} says ”; Use the name variable of this instance in an echo statement..

PHP Workshop ‹#› Class Usage <?php require(‘dog.class.php’); $puppy = new dog(); $puppy->name = ‘Rover’; echo “{$puppy->name} says ”; $puppy->bark(); ?> $puppy->bark(); Use the dog object bark method.

PHP Workshop ‹#› Class Usage <?php require(‘dog.class.php’); $puppy = new dog(); $puppy->name = ‘Rover’; echo “{$puppy->name} says ”; $puppy->bark(); ?> [example file: classes1.php]

PHP Workshop ‹#› One dollar and one only… $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’;

PHP Workshop ‹#› Using attributes within the class.. If you need to use the class variables within any class actions, use the special variable $this in the definition: class dog { public $name; public function bark() { echo $this->name.‘ says Woof!’; } }

PHP Workshop ‹#› Constructor methods 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 __construct name. Remember.. if the constructor requires arguments, they must be passed when it is instantiated!

PHP Workshop ‹#› Constructor Example <?php class dog { public $name; public function __construct ($nametext) { $this->name = $nametext; } public function bark() { echo ‘Woof!’; } } ?> Constructor function

PHP Workshop ‹#› Constructor Example <?php … $puppy = new dog(‘Rover’); … ?> Constructor arguments are passed during the instantiation of the object.

PHP Workshop ‹#› Class Scope 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..

PHP Workshop ‹#› Inheritance The real power of using classes is the property of inheritance – creating a hierarchy of interlinked classes. dog poodlealsatian parent children

PHP Workshop ‹#› Inheritance 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 classes poodle inherits the variable ‘name’ and method ‘bark’ from the dog class, and can add extra ones…

PHP Workshop ‹#› Inheritance example The American Kennel Club (AKC) recognizes three sizes of poodle - Standard, Miniature, and Toy… class poodle extends dog { public $type; public function set_type($height) { if ($height<10) { $this->type = ‘Toy’; } elseif ($height>15) { $this->type = ‘Standard’; } else { $this->type = ‘Miniature’; }

PHP Workshop ‹#› Inheritance example The American Kennel Club (AKC) recognizes three sizes of poodle - Standard, Miniature, and Toy… class poodle extends dog { public $type public function set_type($height) { if ($height<10) { $this->type = ‘Toy’; } elseif ($height>15) { $this->type = ‘Standard’; } else { $this->type = ‘Miniature’; } class poodle extends dog { Note the use of the extends keyword to indicate that the poodle class is a child of the dog class…

PHP Workshop ‹#› Inheritance example … $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(); …

PHP Workshop ‹#› …a poodle will always ‘Yip!’ 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 { … public function bark() { echo ‘Yip!’; } … }

PHP Workshop ‹#› Child Constructors? 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 does not have a constructor, the grandparent constructor is attempted… … etc.

PHP Workshop ‹#› Objects within Objects It is perfectly possible to include objects within another object.. class dogtag { public $words; } class dog { public $name; public $tag; public function bark() { echo "Woof!\n"; } } … $puppy = new dog; $puppy->name = “Rover"; $poppy->tag = new dogtag; $poppy->tag->words = “blah”; …

PHP Workshop ‹#› Deleting objects 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.

PHP Workshop ‹#› A copy, or not a copy.. Entire objects can be passed as arguments to functions, and can use all methods/variables within the function. Remember however.. like functions the object is COPIED when passed as an argument unless you specify the argument as a reference variable &$variable

PHP Workshop ‹#› Why Object Orientate? 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();

PHP Workshop ‹#› Why Object Orientate? 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…

PHP Workshop ‹#› Why Object Orientate? 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.

PHP Workshop ‹#› There is a lot more… We have really only touched the edge of object orientated programming… … but I don’t want to confuse you too much!

PHP Workshop ‹#› PHP4 vs. PHP5 OOP purists will tell you that the object support in PHP4 is sketchy. They are right, in that a lot of features are missing. PHP5 OOP system has had a big redesign and is much better. …but it is worth it to produce OOP code in either PHP4 or PHP5…