Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP 5 Object Oriented Marcus Börger James Cox php|cruise 2004.

Similar presentations


Presentation on theme: "PHP 5 Object Oriented Marcus Börger James Cox php|cruise 2004."— Presentation transcript:

1 PHP 5 Object Oriented Marcus Börger James Cox php|cruise 2004

2 M.Börger, J.CoxPHP 5 Object Oriented2 Overview  PHP 5 vs. PHP 4  Is PHP 5 revolutionary?  PHP 5 OO  Why is OO a good thing?

3 M.Börger, J.CoxPHP 5 Object Oriented3 E = mc 2  PHP 5 is “faster” than PHP 4  Speed by design  Nitty gritty engine improvements  Faster callbacks  Faster comparisons  Faster Harder Stronger  New extensions that eliminate userspace code overhead  PDO  SQLite  PHP 4 executes code faster  New execution architecture slows things down  Execution architecture isn’t terribly important though

4 M.Börger, J.CoxPHP 5 Object Oriented4 Revamped OO Model  PHP 5 has really good OO  Better code reuse  Better for team development  Easier to refactor  Some patterns lead to much more efficient code  Fits better in marketing scenarios

5 M.Börger, J.CoxPHP 5 Object Oriented5 PHP 4 and OO ?  Poor Object model  Methods  No visibility  No abstracts, No final  Static without declaration  Properties  No default values  No static properties  Inheritance  No abstract, final inheritance, no interfaces  Object handling  Copied by value  No destructors

6 M.Börger, J.CoxPHP 5 Object Oriented6 ZE2's revamped object model  Objects are referenced by identifiers  Constructors and Destructors  Static members  Default property values  Constants  Visibility  Interfaces  Final and abstract members  Interceptors  Exceptions  Reflection API  Iterators

7 M.Börger, J.CoxPHP 5 Object Oriented7  Objects are no longer copied by default  Objects may be copied using __clone() __clone(); ?> Objects referenced by identifiers Class Object $obj$ref$dup Instance 1Instance 2

8 M.Börger, J.CoxPHP 5 Object Oriented8  Constructors/Destructors control object lifetime  Constructors may have both new OR old style names  Destructors are called when deleting last reference Constructors and Destructors

9 M.Börger, J.CoxPHP 5 Object Oriented9  Parents must be called manually Constructors and Destructors

10 M.Börger, J.CoxPHP 5 Object Oriented10  Properties can have default values  Bound to the class not to the object  Default values cannot be changed but overwritten prop = "Hello World\n"; $obj2 = new Object; echo $obj2->prop; // Hello ?> Default property values Class Object $prop/default $obj2 Instance 2 $prop $obj1 Instance 1 $prop

11 M.Börger, J.CoxPHP 5 Object Oriented11  Static methods and properties  Bound to the class not to the object  Can be initialized Static members Class Object $stat $obj2 Instance 2 $prop $obj1 Instance 1 $prop

12 M.Börger, J.CoxPHP 5 Object Oriented12 New pseudo constants  __CLASS__shows the current class name  __METHOD__shows class and method or function  Selfreferences the class itself  Parentreferences the parent class  $thisreferences the object itself

13 M.Börger, J.CoxPHP 5 Object Oriented13  Controlling member visibility / Information hiding  A derived class does not know inherited privates  An inherited protected member can be made public Visibility Base $a $b $c Derived $a $b $c Base::$c

14 M.Börger, J.CoxPHP 5 Object Oriented14 Constructor visibility  A protected constructor prevents instantiation  Adding final prevents instantiation of child classes  Static members may call non public constructors

15 M.Börger, J.CoxPHP 5 Object Oriented15 Clone visibility  A protected __clone prevents external cloning  A protected __clone prevents external cloning  A private final __clone prevents cloning  Before __clone is called all properties are copied

16 M.Börger, J.CoxPHP 5 Object Oriented16 Constants  Constants are read only static members  Constants are always public

17 M.Börger, J.CoxPHP 5 Object Oriented17 Abstract members  Properties cannot be made abstract  Methods can be abstract  They cannot have a body (aka default implementation)  A class with an abstract method must be abstract  Classes can be made abstract  Those classes cannot be instantiated <?php abstract class Base { abstract function no_body(); } class Derived extends Base { function no_body() { echo "Body\n"; } } ?>

18 M.Börger, J.CoxPHP 5 Object Oriented18 Final members  Methods can be made final  They cannot be overwritten  They are class invariants  Classes can be made final  They cannot be inherited

19 M.Börger, J.CoxPHP 5 Object Oriented19  Interfaces describe an abstract class protocol  Classes may inherit multiple Interfaces Interfaces Drawable Line Polygon Circle Ellipse ContainerIterator

20 M.Börger, J.CoxPHP 5 Object Oriented20 Property types  Declared properties  May have a default value  Can have selected visibility  Implicit public properties  Declared by simply using them in ANY method  Virtual properties  Handled by interceptor methods  Static properties

21 M.Börger, J.CoxPHP 5 Object Oriented21 Object to String conversion  __toString(): automatic object string conversion

22 M.Börger, J.CoxPHP 5 Object Oriented22 Interceptors  Allow to dynamically handle non class members  Lazy initialization of properties  Simulating Object aggregation, Multiple inheritance

23 M.Börger, J.CoxPHP 5 Object Oriented23 Exceptions  Respect these rules 1.Exceptions are exceptions 2.Never use exceptions for control flow 3.Never ever use exceptions for parameter passing

24 M.Börger, J.CoxPHP 5 Object Oriented24 Exception specialization  Exception must be derived from class exception  Exceptions should be specialized

25 M.Börger, J.CoxPHP 5 Object Oriented25 Exception specialization  Exception blocks can be nested  Exceptions can be rethrown

26 M.Börger, J.CoxPHP 5 Object Oriented26 Constructor failure  Constructors do not return the created object  Overriding $this as in PHP 4 is no longer possible  Exceptions allow to handle failed constructors

27 M.Börger, J.CoxPHP 5 Object Oriented27 Reflection API  Can reflect nearly all aspects of your PHP code  Functions  Classes, Methods, Properties  Extensions

28 M.Börger, J.CoxPHP 5 Object Oriented28 Why else  Simplify situations where a lot of stuff may fail <?php if (@$db=sqlite_open($dbname)) { if (@$res = sqlite_query()) { // handle result if (@$res = sqlite_query()) { // handle result } } } if (sqlite_last_error($db)) { // error handling } ?> <?php try { $db = new sqlite_db($dbname); $res = sqlite_query(); // handle result $res = sqlite_query(): // handle result } catch (sqlite_exception $err) { // error handling } ?>

29 M.Börger, J.CoxPHP 5 Object Oriented29 Iterators  Some objects can be iterated  Others show their properties

30 M.Börger, J.CoxPHP 5 Object Oriented30 Typehinting  PHP 5 allows to easily force a type of a parameter  NULL is allowed with typehints

31 M.Börger, J.CoxPHP 5 Object Oriented31 Iterators  Engine internal Iterator  User Iterators $val) { // accees data } ?> $val) { // access filtered data only } ?> rewind(); $it->valid(); $it->next()) { $value = $it->current(); $key = $it->key(); } ?> rex = $rex; } function accept() { return preg_match($this->rex, $this->current()); } } ?>

32 M.Börger, J.CoxPHP 5 Object Oriented32 New extensions  New OO extensions and state/schedule  FFIPECL / 5.0  DatePECL / 5.1?  DOMbuilt-in, default / 5.0  MySQLibuilt-in / 5.0  PDO5.1?  PIMP5.0?  SimpleXMLbuilt-in, default / 5.0  SOAPbuilt-in / 5.0  SPLbuilt-in, default / 5.0  SQLitebuilt-in, default / 5.0  Tidybuilt-in, default / 5.0  XSLbuilt-in / 5.0

33 M.Börger, J.CoxPHP 5 Object Oriented33 Resources  http://php.net  http://zend.com


Download ppt "PHP 5 Object Oriented Marcus Börger James Cox php|cruise 2004."

Similar presentations


Ads by Google