1 / 33

OOP with PHP

Roman Bednarik rbednari@cs.joensuu.fi. OOP with PHP. 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 = P HP H ypertext P reprocessor Originally a collection of scripts

hisa
Download Presentation

OOP with PHP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Roman Bednarik rbednari@cs.joensuu.fi OOP with PHP

  2. OOP with PHPlecture outline • What is PHP • Why use OOP with PHP • OOP with PHP • Advanced topics on OOP and PHP

  3. OOP 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

  4. OOP 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

  5. OOP 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

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

  7. OOP with PHPlecture outline • What is PHP • Why use OOP with PHP • OOP with PHP • Advanced topics on OOP and PHP

  8. OOP 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!

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

  10. OOP with PHPlecture outline • What is PHP • Why use OOP with PHP • OOP with PHP (or PHP with OOP?) • Advanced topics on OOP and PHP

  11. OOP 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'

  12. <?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 PHPcase class

  13. OOP 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 ($aData1=”default”) { $this -> Data1 = $aData1; }

  14. OOP 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

  15. OOP with PHPInheritance • 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

  16. OOP with PHPPolymorphism • 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”

  17. OOP with PHPconstructors • 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

  18. OOP 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

  19. OOP 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.:

  20. OOP with PHPstatic 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 <br>"; $this->v1++; } var $v1; } $test1 = new Test(); // invokes constructor $test1->printAndIncrease(); $test2 = new Test(); $test2->printAndIncrease();

  21. OOP 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 • => many to be resolved in PHP5

  22. 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_(){} } ?>

  23. 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"; } } ?>

  24. 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< count($this->obj); $i++) { echo $this->obj[$i]->GetCaption(); echo "<BR>";} }

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

  26. OOP with PHPhttp://cs.joensuu.fi/pages/bednarik/OOP/example.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(); ?>

  27. OOP with PHPlecture outline • What is PHP • Why use OOP with PHP • OOP with PHP • Advanced topics on OOP and PHP

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

  29. OOP with PHPSerializing 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!

  30. OOP with PHPOverloading 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();} } ?>

  31. OOP 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( “\””, '' ,$hocus[2] ); // takes away the “

  32. USE OOP! Thank you

More Related