Title: OOP with PHP
1OOP with PHP
- Roman Bednarik
- rbednari_at_cs.joensuu.fi
2OOP with PHPlecture outline
- What is PHP
- Why use OOP with PHP
- OOP with PHP
- Advanced topics on OOP and PHP
3OOP with PHPwhat 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
4OOP with PHPwhat 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
5OOP with PHPwhat 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
6OOP with PHPwhat is PHP? - example
PHP source (example.php) lthtmlgt ltbodygt lt?php A
1 B 2 C A B echo Result of
AB.C echo ltBRgt\n echo Hello
world! ?gt lt/bodygt lt/htmlgt
Browser view Result of 12 3 Hello world!
HTML output lthtmlgt ltbodygt Result of 12
3ltBRgt Hello world! lt/bodygt lt/htmlgt
7OOP with PHPlecture outline
- What is PHP
- Why use OOP with PHP
- OOP with PHP
- Advanced topics on OOP and PHP
8OOP with PHPwhy 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!
9OOP with PHPwhy OOP in PHP examples
- Online shops
- Banking systems
- News services
- Editors' systems
- Home pages
- gt use OOP to separate the functionality from
layout
10OOP with PHPlecture outline
- What is PHP
- Why use OOP with PHP
- OOP with PHP (or PHP with OOP?)
- Advanced topics on OOP and PHP
11OOP with PHPcharacteristics
- PHP fulfills
- Abstract data types
- Information hiding
- Inheritance
- Polymorphism
- PHP fails
- Later on ..
- Almost all can be resolved by some of the
'workarounds'
12OOP with PHPcase class
- lt?php
- class FirstClass
- var x // data member no ways of specifying
private/protected -
- function setX (a)
-
- this-gtx a
-
- function getX ()
- return this-gtx
-
- function printX() echo this-gtx
- // class
- instanceA new FirstClass
13OOP with PHPAbstract 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 (aData1default)
- this -gt Data1 aData1
-
14OOP with PHPInformation 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
15OOP with PHPInheritance
- In PHP using keyword extends
- lt?php
- class SecondClass extends FirstClass
- var Y
- function setY (a)
- if ( getX()gt0) this-gtY a // getX
inherited - else this-gtY 0
-
-
- ?gt
- Multiple inheritance is not supported
16OOP with PHPPolymorphism
- All class member functions are virtual by the
definition - class A
- function draw() echo "1" // not
needed - function boo() this-gtdraw()
-
- class B extends A
- function draw() echo "drawing B"
-
- b new B()
- b-gtboo() // outputs drawing B
17OOP with PHPconstructors
- All initialization has to be done in constructor
- No constructor chaining! gt 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
18OOP with PHPabstract 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
19OOP with PHPstatic 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.
20OOP with PHPstatic variables
- GLOBALS'_transient''static''test'-gtv1 1
// global storage - class Test
- function Test() // constructor
- this-gtv1 GLOBALS'_transient''stat
ic''test'-gtv1 // link a new variable -
- function printAndIncrease()
- echo "this-gtv1 ltbrgt"
- this-gtv1
-
- var v1
-
- test1 new Test() // invokes constructor
- test1-gtprintAndIncrease()
- test2 new Test()
- test2-gtprintAndIncrease()
21OOP with PHPPHP fails on
- No templates
- No private/protected members
- No casting from one class to another
- No static class variables
- No interfaces
- No exceptions
- gt many to be resolved in PHP5
22OOP with PHP
- lt?php
- class Element //basic class for all elements
- function Element(caption"")
- this-gtSetup(caption)
-
- function Setup(caption)
- this-gtcaption caption
-
- function Set() //virtual methods
-
- function Get()
- function GetCaption()
- return this-gtcaption
-
- function Print_()
-
- ?gt
23OOP with PHP
- lt?php
- include_once("class.element.php")
- class Button extends Element //inheritance
- function Button(caption"",action"")
- this-gtSetup(caption,action)
-
- function Setup(caption,action)
- this-gtcaption caption
- this-gtaction action
-
- function Print_()
- echo "ltINPUT TYPEbutton "
- if (this-gtcaption) echo "VALUE\"this-gtcaptio
n\" " - if (this-gtaction) echo "OnClick\"this-gtactio
n\" " - echo "gt\n"
-
-
- ?gt
24OOP with PHP
- lt?php
- include_once("class.element.php")
include_once("class.button.php") - class Page
- var reload
- function Page(caption"",obj)
- this-gtSetup(caption,obj)
- //add the properties and elements common to
all pages - this-gtreload new Button("Reload","document.loca
tion.reload()") -
- function Setup(caption, obj)
- this-gtcaption caption
- this-gtobj obj
-
- function GetCaptions()
- for (i0 ilt count(this-gtobj) i)
- echo this-gtobji-gtGetCaption() echo
"ltBRgt" -
25OOP with PHP
- function print_()
- echo "ltHTMLgt\n ltHEADgt\n ltTITLEgt"
- if (this-gtcaption) echo "this-gtcaption"
- echo " lt/TITLEgt\n"
- echo " lt/HEADgt\n"
- echo " ltBODYgt\n"
- this-gtreload-gtPrint_()
- for (i0 ilt count(this-gtobj) i)
//invoke Print_() for all - //elements on page
- this-gtobji-gtPrint_()
- echo "ltBRgt"
-
- echo " lt/BODYgt\n"
- echo "lt/HTMLgt\n"
-
-
- ?gt
26OOP with PHPhttp//cs.joensuu.fi/pages/bednarik/O
OP/example.php
- lt?php
- include("class.element.php")include("class.button
.php") - include("class.table.php")include("class.page.php
") - tab new Table()
- tab-gtSetup("My table 10x3",10,3)
- tab2 tab
- tab2-gtSetup("My table 2 3x3",3,3)
- but new Button("Click me","alert('Hello')")
- obs0 tab
- obs1 tab2
- obs2 but
- page new Page("Object Page",obs)
- page-gtPrint_()
- page-gtGetCaptions()
- ?gt
27OOP with PHPlecture outline
- What is PHP
- Why use OOP with PHP
- OOP with PHP
- Advanced topics on OOP and PHP
28OOP with PHP
- Generic function for setting the member variables
- function Set (varname, value)
- this-gtvarname value
-
- instance-gtSet ('size','5')
29OOP with PHPSerializing the objects
- Partially overcomes the need for a persistent
object - !!Saves only data members, not methods! (PHP4 is
exception) - lt?php
- myCart new ShoppingCart()
- stream1 serialize(myCart) // and store to
file or db - ...
- ... // retreive from file/db after a year..
- myLaterCart unserialize(stream1)
- ?gt
- Not recommended to use!
30OOP with PHPOverloading in PHP
- lt?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-gtto_call (args) //
variable variable - eval (run)
-
- function ShoppingCart1(x2) code1()
- function ShoppingCart2(x2,y3) code2()
-
- ?gt
31OOP with PHPSerializing 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( \, '' ,hocus2 )
// takes away the
32(No Transcript)
33