OOP with PHP Roman Bednarik

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

PACS – 10/19/131 Object-Oriented Programming “Object-oriented programming opens the door to cleaner designs, easier maintenance, and greater code reuseability.”
Object-Oriented PHP (1)
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Lecture 9 Concepts of Programming Languages
ASP.NET Programming with C# and SQL Server First Edition
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
C++ fundamentals.
Object Oriented Programming using VC++. Introduction Program – Set of instruction written in a high level language High level language used for writing.
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 15: PHP Introduction.
OOP Languages: Java vs C++
PHP: Hypertext Processor Fred Durao
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
PHP TUTORIAL. HISTORY OF PHP  PHP as it's known today is actually the successor to a product named PHP/FI.  Created in 1994 by Rasmus Lerdorf, the very.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Object-Oriented PHP (Chapter 6).
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 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Introduction to PHP Advanced Database System Lab no.1.
PHP Open source language for server-side scripting Works well with many databases (e.g., MySQL) Files end in.php,.php3 or.phtml Runs on all major platforms.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
© 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.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
Object-Oriented Programming Chapter Chapter
Object-Oriented Programming. Objectives Distinguish between object-oriented and procedure-oriented design. Define the terms class and object. Distinguish.
Object Oriented Programming
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.
ISBN Object-Oriented Programming Chapter Chapter
Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Classes, Interfaces and Packages
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.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Advance OOP in PHP.
PHP Training at GoLogica in Bangalore
Object-Orientated Programming
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
Chapter 11 Developing Object-Oriented PHP PHP Programming with MySQL Revised by A. Philipp – Spring 2010 (Rev SP’11)
Java Programming Language
Object-Oriented Programming in PHP
CISC/CMPE320 - Prof. McLeod
Intro to PHP.
Tutorial 6 PHP & MySQL Li Xu
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Lecture 10 Concepts of Programming Languages
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

OOP with PHP Roman Bednarik

OOP with PHP lecture outline What is PHP Why use OOP with PHP OOP with PHP Advanced topics on OOP and PHP

OOP with PHP what is PHP? - development PHP = PHP Hypertext Preprocessor Originally a collection of scripts Developed as open source Major revision in 2001 In 2001 ~ 5 millions of domains using PHP In Oct 2002 ~ 9 millions ! Nowadays in version PHP4 (PHP 4.3RC2) – this lecture PHP5 in development

OOP with PHP what is PHP? - usage Server side scripting generating (not only) HTML Strong association with the Apache server and MySQL Many OS covered Great number of additional modules

OOP with PHP what is PHP? - characteristics Interpreted Loosely type checking Overloading not supported Enables references Case sensitive Variable variable names Associative arrays Serialization of objects into the stream

OOP with PHP what is PHP? - example Browser view Result of 1+2 =3 Hello world! PHP source (example.php) <?php $A = 1; $B = 2; $C = $A + $B; echo “Result of $A+$B=”.$C; echo “ \n”; echo “Hello world!”; ?> HTML output Result of 1+2 =3 Hello world!

OOP with PHP lecture outline What is PHP Why use OOP with PHP OOP with PHP Advanced topics on OOP and PHP

OOP with PHP why OOP in PHP Common OOP approach advantages Modularity Less code Reusability Robustness Handling large projects, easy to maintain Classes usually reflect database schema PHP is not inherently OOP language!

OOP with PHP why OOP in PHP – examples Online shops Banking systems News services Editors' systems Home pages => use OOP to separate the functionality from layout

OOP with PHP lecture outline What is PHP Why use OOP with PHP OOP with PHP (or PHP with OOP?) Advanced topics on OOP and PHP

OOP with PHP characteristics PHP fulfills: – Abstract data types – Information hiding – Inheritance – Polymorphism PHP fails: – Later on.. – Almost all can be resolved by some of the 'workarounds'

OOP with PHP case class <?php class FirstClass { var $x; // data member – no ways of specifying private/protected function setX ($a) { $this->x = $a; } function getX () { return $this->x; } function printX(){ echo $this->x;} } // class $instanceA = new FirstClass; ?>

OOP with PHP Abstract data types Both we have in PHP classes: – Data : integer, string, array, associative array, object – In PHP: no data protection, public is default – Methods: defined as a member functions e.g: function setData1 ($aData1=”default”) { $this -> Data1 = $aData1; }

OOP with PHP Information hiding – encapsulation Good practice is to use set and get methods to access the member structures C++, Java, Ada etc. allow protected/private/public PHP only public by default

OOP with PHP Inheritance In PHP using keyword extends <?php class SecondClass extends FirstClass{ var $Y; function setY ($a) { if ( getX()>0) $this->Y = $a; // getX inherited else $this->Y = 0; } ?> Multiple inheritance is not supported

OOP with PHP Polymorphism All class member functions are virtual by the definition class A { function draw() { echo "1"; } // not needed function boo() { $this->draw(); } } class B extends A { function draw() { echo "drawing B"; } } $b = new B(); $b->boo(); // outputs “drawing B”

OOP with PHP constructors All initialization has to be done in constructor No constructor chaining! => when an object as an instance of derived class is created, only it's constructor is called. If does not exist, parental constructor is called. Solution: – Explicit constructor call – parent:: name_of_parent_class(); No destructors in PHP – automatic garbage collection

OOP with PHP abstract classes Abstract class is not instantiable No standard way in PHP Solution: – Call die in the constructor and methods – if the method is not overridden in a derived class the error occurs

OOP with PHP static variables One instance for all objects of the class No standard way in PHP Workarounds: – Global variables: giving a reference of global variable in each constructor call (creation) – e.g.:

OOP with PHP static variables $GLOBALS['_transient']['static']['test']->v1 = 1; // global storage class Test { function Test() { // constructor $this->v1 = & $GLOBALS['_transient']['static']['test']->v1; // link a new variable } function printAndIncrease() { echo "$this->v1 "; $this->v1++; } var $v1; } $test1 = new Test(); // invokes constructor $test1->printAndIncrease(); $test2 = new Test(); $test2->printAndIncrease();

OOP with PHP PHP fails on No templates No private/protected members No casting from one class to another No static class variables No interfaces No exceptions => many to be resolved in PHP5

OOP with PHP <?php class Element{ //basic class for all elements function Element($caption=""){ $this->Setup($caption); } function Setup($caption) { $this->caption = $caption; } function Set(){ //virtual methods } function Get(){ } function GetCaption(){ return $this->caption; } function Print_(){} } ?>

OOP with PHP <?php include_once("class.element.php"); class Button extends Element { //inheritance function Button($caption="",$action=""){ $this->Setup($caption,$action); } function Setup($caption,$action) { $this->caption = $caption; $this->action = $action; } function Print_() { echo "<INPUT TYPE=button "; if ($this->caption) echo "VALUE=\"$this->caption\" "; if ($this->action) echo "OnClick=\"$this->action\" "; echo ">\n"; } ?>

OOP with PHP <?php include_once("class.element.php"); include_once("class.button.php"); class Page{ var $reload; function Page($caption="",$obj){ $this->Setup($caption,$obj); //add the properties and elements common to all pages $this->reload = new Button("Reload","document.location.reload();"); } function Setup($caption, $obj) { $this->caption = $caption; $this->obj = $obj; } function GetCaptions() { for ($i=0; $i obj); $i++) { echo $this->obj[$i]->GetCaption(); echo " ";} }

OOP with PHP function print_(){ echo " \n \n "; if ($this->caption) echo "$this->caption"; echo " \n"; $this->reload->Print_(); for ($i=0; $i obj); $i++) { / /invoke Print_() for all //elements on page $this->obj[$i]->Print_(); echo " "; } echo " \n"; } ?>

OOP with PHP <?php include("class.element.php");include("class.button.php"); include("class.table.php");include("class.page.php"); $tab= new Table(); $tab->Setup("My table 10x3",10,3); $tab2= $tab; $tab2->Setup("My table 2 3x3",3,3); $but = new Button("Click me","alert('Hello');"); $obs[0] = $tab; $obs[1] = $tab2; $obs[2] = $but; $page= new Page("Object Page",$obs); $page->Print_(); $page->GetCaptions(); ?>

OOP with PHP lecture outline What is PHP Why use OOP with PHP OOP with PHP Advanced topics on OOP and PHP

OOP with PHP Generic function for setting the member variables function Set ($varname, $value) { $this->$varname = $value; } $instance->Set ('size','5');

OOP with PHP Serializing the objects Partially overcomes the need for a persistent object !!Saves only data members, not methods! (PHP4 is exception) <?php $myCart = new ShoppingCart(); $stream1 = serialize($myCart); // and store to file or db // retreive from file/db after a year.. $myLaterCart = unserialize($stream1); ?> Not recommended to use!

OOP with PHP Overloading in PHP <?php class ShoppingCart{ function ShoppingCart(){ $to_call="ShoppingCart".func_num_args(); $args = func_get_args(); // return an array of arguments $args = implode(':',$args); $args = str_replace(“:”, “,”, $args); $run = “\$this->$to_call ($args);”; // variable variable eval ($run); } function ShoppingCart1($x=”2”) { code1();} function ShoppingCart2($x=”2”,$y=”3”) { code2();} } ?>

OOP with PHP Serializing the objects (cont.) Stream is a string with defined format One might try to investigate it: $myCart = new ShoppingCart(); $stream1 = serialize($myCart); $hocus = explode(':',$stream1); // split $stream1 by : into array e.g. $classname = str_replace( “\””, '',$hocus[2] ); // takes away the “

USE OOP ! Thank you