260 likes | 342 Views
New Features in PHP5. Paul F. Gilzow Web Communications. Backward Incompatible Changes. array_merge () only accepts arrays strrpos () and strripos () now use the entire string as a needle
E N D
New Features in PHP5 Paul F. Gilzow Web Communications
Backward Incompatible Changes • array_merge() only accepts arrays • strrpos() and strripos() now use the entire string as a needle • get_class(), get_parent_class() and get_class_methods() now return the name of the classes/methods as they were declared (case-sensitive) • An object with no properties is no longer considered "empty"
Backward Incompatible Changes cont. • New reserved keywords
New Features • Exceptions • SimpleXML • DOM • Objects • SQLite Support • PDO • Reflection Class • JSON Support • New Functions
Exceptions • PHP5 introduces exceptions using a Java-like syntax: try, catch, throw • All exceptions extend the standard Exception class, so custom exceptions are possible
SimpleXML • Very simple, built-in XML parser • Converts XML to an object that can be processed with normal property selectors and array iterators
DOM DOM is a complete rewrite of the DOMXML extension. The new extensions makes use of PHP 5's OO features. • DOM Level 3 Load and Save Support • XPath • Validation support • NamedNodeMap support • Complete Namespaces support • Interop with SimpleXML and XSLT • HTML Support
Objects • Referencing • Clone() • Access Controls • Constructor and Destructor • Class constants • Getters and setters • Class Constants (aka class-level defines)
Object Referencing • In PHP4, if you pass an object to function, it would create an internal copy. In order to access the object, you would have to pass it in by reference. • In PHP5, objects are automatically referenced
Object Cloning • Use the clone keyword to make a copy of an object
Object Access Controls • In PHP4, there was no concept of object property protection. Any chunk of code could modify a member variable’s value • PHP5 introduces the following access controls to both methods and properties • Public • Protected • Private
Object Access Controls - Public • This was default for all methods and properties in PHP4 • Accessible everywhere
Object Access Controls - Protected • Limits access to • The class that defines them • Inherited classes • Parent classes
Object Access Controls - Private • 'Strongest' access level • Can only be accessed/called from the same class • Force use of get()/set() functions to access properties
Objects – Constructors/Destructors • Unified constructor name: __construct() • In PHP4 the constructor was named the same as the class • Unified destructor name: __desctruct() • Destructor method is called when all references to the object are removed, when the object is explicitly destroyed, or in script shutdown • Destructors called during the script shutdown have HTTP headers already sent. • Attempting to throw an exception from a destructor (called in the time of script termination) causes a fatal error
Objects – Class Constants • Supports declaring class-specific constant values • Similar to define in PHP4
SQLite • SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine • Bundled with PHP5 • Fast and easy interface to ‘flat file’ • SQL 99 subset
PDO • PHP Data Objects • A lightweight set of PHP extensions that provide a core PDO class and database specific drivers • Creates a unified interface for creating and maintaining database connections, issuing queries, quoting parameters, traversing result sets, dealing with prepared statements and error handling • PDO provides a data-access abstraction layer, which means that regardless of which database you are using, you use the same functions to issue queries and fetch data
PDO – What Can It Do? • Prepare/execute, bind parameters • Transactions • LOBs • Flexible error handling • Smooth over database specific nuances
PDO – Supported Databases • Oracle • MSSQL • MySQL • PostgreSQL • SQLite • ODBC/DB2 • Firebird/Interbase
Reflection • Also referred to as “Introspection” • Provides the ability to reverse-engineer • Classes • Interfaces • Functions • Methods • Extensions • Also provides method to retrieve doc comments
JSON Support • Javascript Object Notation data-interchange format encoder/decoder • As of PHP 5.2.0, the JSON extension is bundled and compiled into PHP by default • Can decode a JSON string to a PHP object or associative array • Can encode any PHP type (except resource) into a JSON encoded string
New Functions - Array • array_combine() - Creates an array by using one array for keys and another for its values • array_diff_uassoc() - Computes the difference of arrays with additional index check which is performed by a user supplied callback function • array_udiff() - Computes the difference of arrays by using a callback function for data comparison • array_uintersect_assoc() - Computes the intersection of arrays with additional index check. The data is compared by using a callback function • array_uintersect_uassoc() - Computes the intersection of arrays with additional index check. Both the data and the indexes are compared by using separate callback functions
New Functions – Date/Time related • DateTime() – OO API to date/time • Date_default_timezone_get()/set() – Gets/Sets the default timezone used by all date/time functions in a script • idate() - Format a local time/date as integer • date_sunset() - Time of sunset for a given day and location • date_sunrise() - Time of sunrise for a given day and location • time_nanosleep() - Delay for a number of seconds and nanoseconds
New Functions - Misc • __autoload() • php_check_syntax()
PHP5 On Campus • The Division of IT is working on a plan to upgrade the Departmental Web Hosting Service to the latest release of Red Hat Enterprise which includes PHP5. The steps required are: • Upgrade the Virtual Host Development server and allow departments up to 30 days to test their pages and applications to ensure they work properly with the new operating system and PHP5 • Schedule a service outage and upgrade the production servers • If code or application modifications are required, departmental users will move their changes from development to production after the upgrade outage • Given that the next logical time frame for the production upgrade is Christmas break/intercession, we would like to have this entire process completed after December 19 but no later than January 18, 2009. That means we can start the upgrade on the development server in November.