oriolrius.cat

Des del 2000 compartiendo sobre…

Tag: php4-vs-php5

PHP4 vs PHP5

Reading time: 2 – 3 minutes

Tot llegint el PHP|Architect’s Guide to PHP Design Patterns (isbn: 0973589825) vaig trobar fa un parell de dies un petit llistat de quines eres les diferencies fonamentals entre PHP4 i PHP5, a part del tòpic que amb el motor de Zend 2.1 ara tenim suport de OOP i de que tothom em digui que el model d’objectes que usa PHP5 encara és molt dolent doncs bé amb això podrem saber fins a quin punt ho és.

  • Object handles
  • Better constructors (uniform name, changing $this not allowed)
  • Destructors now exist
  • Visibility (public, protected, private for methods and attributes)
  • Exceptions (an alternative to triggering errors using the new try{} catch{} syntax)
  • Classconstants (defines using the class for a name space)
  • Reflection (dynamic examination of classes, methods and arguments)
  • Type hinting (specifying expected classes or interfaces for method arguments)

També hi podem trobar altres novetats una mica menys conegudes:

  • New magic methods (__get() and __set() allow you to control attribute access; __call() lets you dynamically intercept all method calls to the object; __sleep() and __wakeup() let you override serialization behavior; and __toString() lets you control how an object represents itself when cast as a string)
  • Autoloading (allows the end user to try to automatically load the class the first time a reference to it is made)
  • Final (do not allow a method or a class to be overridden by subclasses)

A més hi ha coses com la part d’object handles que donen molt de si:

  • Create an object by reference, as in $obj =& new Class;
  • Pass an object by reference, like function funct(&$obj_param) {}
  • Catch an object by reference function &some_funct() {} $returned_obj =&
    some_funct()

En escència el que més m’ha agradat ho teniu resumit aquí, ja sé que esta en anglès però em feia una mandra brutal traduir-ho. Si m’animo un dia d’aquests donaré més detalls de com aprofitar aquestes avantatges per implementar design patterns amb PHP5. Realment ja comences a sentir-te com un programador de veritat quan veus que el PHP5 no té cap problema en suportar estructures que fins ara li quedaben tan lluny.