PHP 5

956 views
899 views

Published on

Presentation explains the features that php5 has to offer.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
956
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
33
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

PHP 5

  1. 1. PHP FoundationsRafael Corral, Lead Developer corePHP CMS Expo 2011
  2. 2. What is New  Object model  Assignments and Object Copying  Constructors  Standard PHP Library (SPL)  New Functions / Extensions  Much More.. .
  3. 3. Object Model  Completely rewritten  Reference  Visibility  OO Classes  Constants  Magicness  __autoload
  4. 4. Passed by Reference  All objects are now passed by reference  To copy an object use the clone keyword  Stop using the & operator http://us.php.net/manual/en/language.oop5.references.php!
  5. 5. Class Properties Constant Static  Per-class basis   Can be applied to variables  Accessed by :: operator and methods  No $ symbol to access   $this cannot be used within a  The value must be an static method expression only   Static variables are shared  No initialization required between subclasses   No initialization required http://php.net/manual/en/language.oop5.constants.php! http://php.net/manual/en/language.oop5.static.php!
  6. 6. Visibility  Class methods and properties now have visibility  Three levels of visibility   Public  Most visible, anyone can access. Read/Write   Protected  Access by subclasses, parent and itself   Private  Access only by class itself http://php.net/manual/en/language.oop5.visibility.php!
  7. 7. Construct or Destruct  Unified constructor/destructor names  Prefixed by two (2) underscores (__)  __construct()   Method works the same as on PHP4   Backwards Compatible  __destruct()   All references to object are destroyed   Script shutdown phase  To run parents, one must use parent::__construct() http://php.net/manual/en/language.oop5.constants.php!
  8. 8. Abstract Classes  Cannot be initiated  Used as a blue print for other classes to extend  Any class with abstract methods, must be abstract  Subclasses must declare abstract methods from parent   These must be defined with less or equal visibility http://php.net/manual/en/language.oop5.abstract.php!
  9. 9. Interfaces  Used to design APIs  Defines the methods a class should implement  Not a blueprint but a way to standardize an API  A Class can implement any number of them   Unlike extending to only one  All methods must be public  Methods don’t have any content defined http://php.net/manual/en/language.oop5.interfaces.php!
  10. 10. cont... Interfaces  Use implement operator implement in class  Use interface to declare interface  A Class can implement more than one interface  Interfaces can be extended with extend  Implemented interfaces cannot share function names  Interfaces can have constants just like classes
  11. 11. Magic Methods  These methods are called “automatically”  These are prefixed by two (2) underscores (__)  __construct, __destruct, __call, __callStatic, __get, __set, __isset, __unset, __sleep, __wakeup, __toString, __invoke, __set_state and __clone http://php.net/manual/en/language.oop5.magic.php!
  12. 12. More Magicness  __sleep and __wakeup   Run before Object is serialized/ unserialized  __toString   Run when class is converted to string  __call and __callStatic   Run when invoking inaccessible methods
  13. 13. Finality  PHP5 introduces the final keyword  Can be applied to methods and classes  For methods -   It prevents subclasses from overriding them  For classes -   States that it is the final class, no subclasses http://php.net/manual/en/language.oop5.final.php!
  14. 14. Finality
  15. 15. __autoload()  Used to automatically load PHP files  Runs when PHP encounters an undefined class  Can be useful instead of having many include()’s  The SPL library contains a similar function http://php.net/manual/en/language.oop5.final.php!
  16. 16. Standard PHP Library  Functions to solve problems  Class Implements  Class Parents Just cool functions
  17. 17. About  The SPL library is a collection of interfaces and classes  These are meant to help solve standard problems http://www.php.net/manual/en/intro.spl.php!
  18. 18. Some Functions  class_implements( mixed $class )   Returns all interfaces implemented by a class  class_parents( mixed $class )   Returns all parent classes for a given class  spl_autoload_register([ callback $autoload_function ])   Better than using __autload()   Can register multiple functions to load class files http://www.php.net/manual/en/ref.spl.php!
  19. 19. ArrayObject  Turns an array into an object  This class contains helpful functions such as:   Counting elements   Finding a key   Get key   Set key   Unset key http://www.php.net/manual/en/class.arrayobject.php!
  20. 20. SplFileInfo  OO Class to get information about an individual file  Some included functions are:   Last access time   Base name   Target of a link   Get owner/permissions   Size/File type   If directory   isReadable/isWritable http://www.php.net/manual/en/class.splfileinfo.php!
  21. 21. Other FeaturesMisc.  Type Hinting  Exceptions  foreach By-Reference  New Functions  New Extensions
  22. 22. Type Hinting  Enforce the type of variable passed to functions   Functions and Class functions  Can only be used for classes or arrays (at this time)   No other scalar types (integer, string)  If null is the default parameter value   It is allowed as an argument http://www.php.net/manual/en/class.splfileinfo.php!
  23. 23. Exceptions  Exceptions are just errors  Used to catch exceptions  Gain control over error notices  Use when executing “risky” code  They work similar to other programming languages  Each try{} must have a corresponding catch{} block http://php.net/manual/en/language.exceptions.php!
  24. 24. E_strict  In PHP5 a new error level is introduced E_STRICT  It is not included within E_ALL  Value 2048  To call it: error_reporting( E_ALL ^ E_STRICT );  Triggered when using deprecated code   Useful in development environment http://us3.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting!
  25. 25. foreach by-reference  Reference to value while looping through foreach  Careful:   After foreach is done reference won’t go away   Use unset to remove reference http://us2.php.net/foreach!
  26. 26. New Functions  array_combine()  array_walk_recursive()  time_nanosleep()  str_split()  strpbrk()  substr_compare()  curl_copy_handle()  file_put_contents()  get_headers()  headers_list()  http_build_query()  php_strip_whitespace()  scandir()  a lot more... http://php.net/manual/en/migration5.functions.php!
  27. 27. New Extensions  Simple XML   Processing of XML (pretty simple)  DOM and XSL   Building XML/XSL files  Hash Functions   Library of hash functions (more than md5 or sha1)
  28. 28. Compatibility  array_merge()   Gives error if one of the parameter is not an array   array_merget( (array) $var1, (array) $var2 );  To copy objects you must use the clone keyword  get_class(), get_parent_class(), get_class_methods()   Are not case sensitive  An object with no properties is no longer considered “empty”  strpos() and strripos()   Now use the entire string as a needle
  29. 29. Let go of PHP4 and start developing in PHP5
  30. 30. Questions?
  31. 31. Thank You!Rafael Corral!Email: rafael@corephp.com!Skype: rafa.corral!

×