PHP 5 Bootcamp for PHP 4 Programmers Adam Trachtenberg eBay Technical Evangelist [email_address]
Before we begin I know it’s early in the morning We’ll start slowly Ask lots of questions Don’t worry about interrupting Tell me to talk slower I’m from New York, so I talk too fast Enjoy!
 
Tutorial Structure PHP 5 is new, and so is this tutorial. Lots of information, so I will try and shape the lesson around your wants. Two primary objectives Highlight new features Tip you off towards incompatibilites Code samples use “Before & After” when applicable.
Warm Up Who knows PHP 4 PHP 5 OOP MySQL MySQL 4.1? XML DOM XPath
Plan of Attack Object-Oriented Programming Databases MySQL SQLite XML DOM SimpleXML Everything else
Object-Oriented Programming Group functions and data together. Encourages encapsulation. And code reuse. Not so hot in PHP 4 Much better in PHP 5
Object-Oriented Programming References Constructors
Object-Oriented Programming References Constructors & Destructors Visibility Interfaces & Abstract Classes Type Hints Static Methods & Properties Final Methods, Properties, & Classes Magical Methods
Basic Concepts Class Property Method Object Instantiation (Constructors) Encapsulation Inheritance / Subclassing Extends Is a relationship Specialization
References Objects in PHP5 passed by reference. A reference is a pointer to the variable. Any alterations made to the passed object are actually made to the original.  Force pass by reference using “&”. Applies to assignments and function/method parameters.
PHP 4 vs PHP 5 $rasmus = new Person;  $rasmus->setName( 'Rasmus Lerdorf'); $zeev = $rasmus; $zeev->setName( 'Zeev Suraski'); print $rasmus-> getName(); print $zeev->getName(); Rasmus Lerdorf Zeev Suraski
PHP 4 vs PHP 5 $rasmus = new Person;  $rasmus->setName( 'Rasmus Lerdorf'); $zeev = $rasmus; $zeev->setName( 'Zeev Suraski'); print $rasmus-> getName(); print $zeev->getName(); Rasmus Lerdorf Zeev Suraski $rasmus = new Person;  $rasmus->setName( 'Rasmus Lerdorf'); $zeev = $rasmus; $zeev->setName( 'Zeev Suraski'); print $rasmus-> getName(); print $zeev->getName(); Zeev Suraski Zeev Suraski
PHP 5 vs PHP 5 w/clone $rasmus = new Person; $rasmus->setName( 'Rasmus Lerdorf');  $zeev = $rasmus; $zeev->setName( 'Zeev Suraski'); print $rasmus-> getName(); print $zeev->getName(); Zeev Suraski Zeev Suraski
PHP 5 vs PHP 5 w/clone $rasmus = new Person; $rasmus->setName( 'Rasmus Lerdorf');  $zeev = $rasmus; $zeev->setName( 'Zeev Suraski'); print $rasmus-> getName(); print $zeev->getName(); Zeev Suraski Zeev Suraski $rasmus = new Person; $rasmus->setName( 'Rasmus Lerdorf');  $zeev = clone $rasmus; $zeev->setName( 'Zeev Suraski'); print $rasmus-> getName(); print $zeev->getName(); Rasmus Lerdorf Zeev Suraski
Constructors Same as PHP 4, but new naming scheme __construct() Reduces brittleness Will revert to old PHP 4 style naming scheme
PHP 4 vs PHP 5 class Database { function Database($h) { $this->h = db_connect($h); } } $db = new  Database('db.example.com');
PHP 4 vs PHP 5 class Database { function Database($h) { $this->h = db_connect($h); } } $db = new  Database('db.example.com');  class Database { function  __construct ($h) { $this->h = db_connect($h); } } $db = new Database('db.example.com');
Destructors New in PHP 5 __destruct() Run whan last instance of object destroyed This is probably before the end of the request Much cooler than PHP 4 kludge using  register_shutdown_function()
PHP 4 vs PHP 5
PHP 4 vs PHP 5 class Database { function __destruct() { db_close($this->h); } }
Visibility Access restrictions on properties and methods Public: everyone (Replaces  var ) Protected: class and descendents Private: class  only Enforces encapsulation Distinction between public and private Public interface stays constant Private (internal) guts can be safely modifed at will
PHP 4 vs PHP 5 class Person { var $name; function  setName($name) { $this->name = $name; } } $rasmus = new Person; $rasmus->setName( 'Rasmus Lerdorf'); print $rasmus->name; Rasmus Lerdorf
PHP 4 vs PHP 5 class Person { var $name; function  setName($name) { $this->name = $name; } } $rasmus = new Person; $rasmus->setName( 'Rasmus Lerdorf'); print $rasmus->name; Rasmus Lerdorf class Person { private  $name; public  function setName($name) { $this->name = $name; } } $rasmus = new Person; $rasmus->setName( 'Rasmus Lerdorf'); print $rasmus->name; PHP Fatal error:  Cannot access private property Person::$name
Interfaces The mechanism for forcing classes to support the same set of methods. Alternative to multiple inheritance. Used in Java (versus C++). Classes  implement  one or more interfaces. If a class promises to implement an interfaces, but doesn’t, it’s a fatal error. Pre-declare interfaces to keep PHP 5 happy.
Sellable Interface interface Sellable { public function getName(); public function getPrice(); } class Book implements Sellable { public function getName() { ... } public function getPrice() { ... } }
Abstract Classes Similar to interfaces, but integrated into object heirarchy. Abstract base class, concrete children. Cannot be directly instantiated. The  is a  relationship applies.
Abstract Database Class 1/2 abstract  class Database { abstract  public function connect(); abstract  public function close(); }
Abstract Database Class 2/2 class MySQL extends Database { protected $dbh; public function connect($s) { $this->dbh = mysql_connect($s); } public function close() { mysql_close($this->dbh); } }
Type Hints Require parameter must be of a certain class. Uses  instanceof  check. (Subclasses okay.) Only  works for objects. Cannot pass  NULL . Violations cause a  fatal  error.
PHP 4 vs PHP 5 class AddressBook { function add($person) { if (!(is_a( $person, ‘Person’)) { die("Argument 1 must be an instance of Person"); } // add $person to // address book } }
PHP 4 vs PHP 5 class AddressBook { function add($person) { if (!(is_a( $person, ‘Person’)) { die("Argument 1 must be an instance of Person"); } // add $person to // address book } }  class AddressBook { public  function add( Person  $person) { // add $person to // address book } }
Static Methods and Properties Static methods Callable without instantiation Use  ::  instead of  -> Cannot refer to  $this Static properties Class namespaced global properties Shared among all instances of a class Also use  ::  instead of  -> Use  self  instead of  $this
Final Classes, Methods, and Properties Final Classes Prevent subclassing of entire object final class MySQL { … } Final Methods Prevent subclassing of individual method Even if all methods are  final , can still subclass object final public function query($q) { … }  Final Properties Object constants class Math {  const pi = 3.14159; } $area = Math::pi * $radius * $radius;
Magical Methods __construct() __destruct() __get() __set() __call() __toString() __clone()
__get() and __set() Called when you try to read or write to an object property. Simplies property accessors. Screws up property inheritance. Only called for nonexistent properties. This applies to private and protected properties too!
Accessors class Person { private $data; public function __get($property) { return $this->data[$property]; } public function __set($property, $value) { $this->data[$property] = $value; } } $rasmus = new Person; $rasmus->name = 'Rasmus Lerdorf'; print $rasmus->name; Rasmus Lerdorf
__call() Like  __get()  and  __set() , but for methods. Captures any undefined methods. Used for object aggregation. And for dynamic proxy objects where the methods aren’t known ahead of time (like a SOAP client). Also screws up inheritance.
__toString() Allows you to define a “pretty print” version of an object. Only called with  print  and  echo . Won’t work with Concatenation ( . ) Casting ( strval() ,  (string) ) Functions that take strings Work around: explicitly call  __toString()   Should be “smarter” in PHP 5.1
PHP 4 vs PHP 5 class Rasmus { function toString() { return “ Rasmus Lerdorf”; } } $rasmus = new Rasmus; print $rasmus-> toString(); Rasmus Lerdorf
PHP 4 vs PHP 5 class Rasmus { function toString() { return “ Rasmus Lerdorf”; } } $rasmus = new Rasmus; print $rasmus-> toString(); Rasmus Lerdorf class Rasmus { public  function __ toString() {  return “ Rasmus Lerdorf”; } } $rasmus = new Rasmus; print  $rasmus; Rasmus Lerdorf
__clone() Called when you use clone operator. __clone()  lets you override normal behavior. By default, PHP 5 does “shallow copy.” You may want “deep copy” or to otherwise control the cloning process. Given  $this  to play with.
Databases MySQL New MySQLi extension “ I” stands for “improved!” MySQL 4.1.2+ SQLite Database library Bundled with PHP 5 “ Flatfiles be gone!”
MySQL Old MySQL extension showing its age. MySQL 4.1 introduced new client protocol. Time for a rewrite! Something old, something new… Some functions deleted Some functions changed Some functions added Porting isn’t  s/mysql_/mysqli_/g;
New Syntax Functions begin with  mysqli_ No more default links. Database handle always comes first. mysqli_connect()  has different prototype. No more persistant connections. Or  mysql_escape_string() .
MySQL vs MySQLi $db = mysql_connect( $server, $user, $pass); mysql_select_db( $db, "users");  $r = mysql_query( "SELECT user FROM users", $db); while ($row = mysql_fetch_assoc($r)) { print $row['user']; } mysql_free_result($r); mysql_close($db);
MySQL vs MySQLi $db = mysql_connect( $server, $user, $pass); mysql_select_db( $db, "users");  $r = mysql_query( "SELECT user FROM users", $db); while ($row = mysql_fetch_assoc($r)) { print $row['user']; } mysql_free_result($r); mysql_close($db); $db = mysql i _connect( $server, $user, $password , "users" ); $r = mysql i _query( $db, "SELECT user FROM users"); while ($row = mysql i _fetch_assoc($r)) { print $row['user']; } mysql i _free_result($r); mysql i _close($db);
OO Interface First class database object PHP 5 feature Extendable Clearly separate multiple connections Cool. :)
Procedural vs OO $db = mysqli_connect( $server, $user, $password, "users"); $r = mysqli_query( $db, "SELECT user FROM users"); while ($row = mysqli_fetch_assoc($r)) { print $row['user']; } mysqli_free_result($r); mysqli_close($db);  $db =  new mysqli ( $server, $user, $password, "users"); $r =  $mysqli->query ( "SELECT user FROM users"); while ($row = $r->fetch_assoc ()) { print $row['user']; } $r->close(); unset($db);
Prepared Statements Speeds up MySQL query processing. Query template with placeholders. Bind PHP variables into MySQL input. Bind MySQL ouput to PHP variables. Automatically escape quotes. Prevent security vulnerabilities.
Traditional // User-entered data $user = 'rasmus'; $pass = ’carl'; $zip  = 94088; // Escape data $user = mysqli_real_escape_string($db, $user); $pass = mysqli_real_escape_string($db, $pass); $zip  = intval($zip); // Create SQL query $sql = "INSERT INTO users VALUES ('$user, '$pass, $zip)"; // Make SQL query mysqli_query($db, $sql);
Stored Procedure // User-entered data $user = 'rasmus'; $pass = 'carl'; $zip  = 94088; // Prepare statement $sql = 'INSERT INTO users VALUES(?, ?, ?)'; $stmt = mysqli_stmt_init($db); if (mysqli_stmt_prepare($stmt, $sql)) { // Bind placeholders w/ types and var names // 'ssi’: two strings and one integer mysqli_stmt_bind_param($stmt, 'ssi', $user, $pass, $zip); // Execute statement  mysqli_stmt_execute($stmt); }
Bound Output Parameters $sql = 'SELECT username FROM users'; $stmt = mysqli_stmt_init($db); if (mysqli_stmt_prepare($stmt, $sql)) { // Bind result variables mysqli_stmt_bind_result($stmt, $username); mysqli_stmt_execute($stmt); // Place query data into bound result variables while (mysqli_stmt_fetch($stmt)) {   print "$username\n"; } } ramsus zeev
Subselects Embed one query inside another Places logic inside MySQL instead of PHP Useful way of winnowing data Can often be avoiding by doing a self-join But are sometimes necessary
Transactions Group related queries into a single unit Database commits all or none Preserves consistency and integrity Useful in many cases Vital in banking when you can’t have two transactions cross in mid-stream
Multi-Query Issue multiple queries within a single call Only available with new MySQL 4.1 client protocol. Disabled for  mysqli_query() Lots of work for minimal gain Only really useful if you’re writing something like  phpMyAdmin
SSL Encrypt communication between PHP and MySQL using SSL Bit of a pain to set up if you’re unfamiliar with SSL Need to build PHP and MySQL with OpenSSL and relink PHP to SSL-enabled MySQL
Migrating Should you migrate? Harder then you think: PHP 4 to PHP 5 MySQL 3.x (or 4.0) to MySQL 4.1 MySQL to MySQLi Do it if you have to: MySQL 4.1 On going project
Plan of Attack Move to MySQL 4.1.2 Move to PHP 5 Basic MySQLi Port Use MySQLi Specific Features
SQLite Database in a box Always available Eliminates locking nightmares Faster searching Not actually “Lite” But it has some limitations
Basics Databases are files Opening a database creates the file Typeless You can declare, but SQLite ignores (except for sorting). Not all that crazy. PHP is typeless and we like that, don’t we? Operates like most of DB extensions
Example $db = sqlite_open('/www/support/users.db'); sqlite_query($db,  'CREATE TABLE users(username VARCHAR(100) password VARCHAR(100))'); $username = sqlite_escape_string($username); $password = sqlite_escape_string($password); sqlite_query($db, "INSERT INTO users VALUES ('$username', '$password')"); $r = sqlite_query($db, 'SELECT username FROM users'); while ($row = sqlite_fetch_array($r)) { // do something with $row }
OO Interface $db =  new SQLiteDatabase ('/www/support/users.db'); $db->query ( 'CREATE TABLE users(username VARCHAR(100), password VARCHAR(100))'); $username = sqlite_escape_string($username); $password = sqlite_escape_string($password); $db->query ("INSERT INTO users VALUES ('$username', '$password')"); $r =  $db->query ('SELECT username FROM users'); while ($row =  $r->fetch() ) { // do something with $row }  unset($db);
Iterators // one at a time $r = $db->query('SELECT * FROM users'); foreach ($r as $row) { // do something with $row }
In-Memory Tables Create an in-memory table by using the filename:  :memory: Keeps the table in RAM for fast access. Doesn’t persist between requests. Best for one-time processing. Or when you need to make lots of queries.
Transactions SQLite supports transactions
UDFs Write UDFs in PHP and register them with SQLite. Works for both standard and aggregate functions.
XML Work together as a unified whole Are standardized on a single XML library:  libxml2 Fully comply with W3 specifications Efficiently process data Provide you with the right XML tool for your job
XML Extensions DOM XSLT SAX SimpleXML XPath XML Pull Parser (PECL)
Document Object Model: DOM The 800-pound gorilla of XML. You can do everything and the <kitchen-sink> with DOM. Navigating through documents can be cumbersome. Tree-based API, must load entire document into memory. Much better than PHP 4 DOMXML extension, but not backwards compatible.
XSLT Transform XML documents into HTML XML (RSS, SOAP, etc.) Anything else Uses XML-based stylesheets. Easily shared among different applications. Has a quirky syntax.  Uses libxslt instead of Sablotron.
Simple API for XML: SAX PHP’s original XML extension. Streaming, or event-based, parser Uses less memory than DOM, but frequently requires more complex PHP code.  Now uses libxml2 instead of expat. Translation layer provides compatability. Almost does a good job. :)
SimpleXML New PHP 5–only extension Excels at parsing RSS files, REST results, and configuration data. If you know the document’s format ahead of time, SimpleXML is the way to go. However, SimpleXML supports only a subset of the XML specification. If you only remember one XML topic from this talk, it should be SimpleXML.
XPath Regular expressions for XML documents. Isolate the data you want. Used in XSLT. Also availble in DOM and SimpleXML If you only remember two XML topics from this talk, it should be SimpleXML and XPath.
Reading: DOM: PHP 4 $dom = domxml_open_file('address-book.xml'); foreach ($dom->get_elements_by_tagname('person') as $person) { $firstname = $person-> get_elements_by_tagname('firstname');  $firstname_text = $firstname[0]->first_child();  $firstname_text_value = $firstname_text-> node_value(); print &quot;$firstname_text_value\n&quot;; }
Reading: DOM: PHP 5 $dom = new DOMDocument; $dom->load('address-book.xml'); foreach ($dom->getElementsByTagname('person') as $person) { $firstname = $person-> getElementsByTagname('firstname'); $firstname_text_value = $firstname->item(0)-> firstChild->nodeValue; print &quot;$firstname_text_value\n&quot;; }
What’s different? studlyCaps get_elements_by_tagname() getElementsByTagname() DOM node list vs array $firstname[0] $firstname->item(0)  Methods vs Properties first_child() firstChild Object vs Resource
SimpleXML $sx = simplexml_load_file( 'address-book.xml'); foreach ($sx->person as $person) { print $person->firstname \n&quot;; }
Searching: PHP 4 $dom = domxml_open_file( 'address-book.xml'); $xpath = xpath_new_context($dom); $emails = $xpath->xpath_eval( '/address-book/person/email'); foreach ($emails->nodeset as $e) { $tmp = $e->first_child(); $email = $tmp->node_value(); // do something with $email }
Searching: PHP 5 & XPath $dom = new DOMDocument; $dom->load('address-book.xml'); $xpath = new DOMXPath($dom); $emails = $xpath->query( '/address-book/person/email'); foreach ($emails as $e) { $email = $e->firstChild-> nodeValue; // do something with $email }
Searching: PHP 5 & SimpleXML $s = simplexml_load_file( 'address-book.xml'); $emails = $s->xpath( '/address-book/person/email'); foreach ($emails as $email) {   // do something with $email }
Transforming Pass DOM objects Nicer, cleaner, faster libxslt  instead of Sablotron
PHP 4 vs PHP 5 $xml = 'data.xml'; $xsl = 'style.xsl'; $xslt = xslt_create(); $results = xslt_process($xslt,  $xml, $xsl); xslt_free($xslt);
PHP 4 vs PHP 5 $xml = 'data.xml'; $xsl = 'style.xsl'; $xslt = xslt_create(); $results = xslt_process($xslt,  $xml, $xsl); xslt_free($xslt);  $xml = new DOMDocument; $xml->load('data.xml'); $xsl = new DOMDocument; $xsl-> load('style.xsl'); $xslt = new XSLTProcessor(); $xslt-> importStylesheet($xsl); $results = $xslt-> transformToXML($xml);
Everything else Iterators and SPL Streams Exceptions SOAP Reflection Tidy
Iterators Allow you to loop through objects using  foreach()  in a controlled fashion. Encapsulate looping logic inside class. Reduces errors More powerful behaviors SPL provides some generic classes. And some examples.
Functions vs Iterators $d = opendir($path); while ( $file = readdir($d)) { print &quot;$file\n&quot;; } closedir($d);
Functions vs Iterators: Take 2 $d = opendir($path); while ( false !==  ($file=readdir($d))) { print &quot;$file\n&quot;; } closedir($d);
Functions vs Iterators $d = opendir($path); while ( false !==  ($file=readdir($d))) { print &quot;$file\n&quot;; } closedir($d);  foreach ( new DirectoryIterator( $path) as $file) { print &quot;$file\n&quot;; }
Streams, Wrappers, and Filters Place file interface upon protocols Allow fopen() and friends to “speak” Files http ftp Available in PHP 4.3, but not widely used PHP 5 offers new and improved version Can implement custom wrappers and filters in C and PHP
Exceptions New way of error handling Instead of checking return values, you “catch” error objects. Used in Java Can be used for good… and for evil! PHP 5 provides complete framework, but not well-baked into core, key extensions.
PHP 4 $version = '1.0'; $element = 'address-book'; $dom = domxml_new_doc($version); $ab = $dom->create_element($element); $ab = $dom->append_child($ab);
PHP 4 w/Error Checking $version = '1.0'; $element = 'address-book'; if ($dom = domxml_new_doc($version)) { if ($ab = $dom->create_element($element)) { if ($ab = $dom->append_child($ab)) { // Successfully appended address-book element } else { // Cannot append child } } else { // Cannot create <address-book> element } } else { // Cannot create new document }
PHP 5 w/Exceptions $version = '1.0'; $element = 'address-book'; try { $dom = new DOMDocument($version); $ab = new DOMElement($element); $ab = $dom->appendChild($ab); } catch (DOMException $e) { print $e; }
Exception Class Populated w/data by PHP about the error Message, Code, Line, File, Backtrace __toString()  produces formated message
SOAP Key component of Web services PHP 4 only offers PHP-based packages PHP 5 bundles C-based extension! Not enabled by default Uses libxml2 SOAP works hard to make things easy.  Semi-officially sponsored by Zend. SOAPClient and SOAPServer
SOAPClient Request $wsdl_url =  'http://www.xmethods.net/sd/2001/'. 'TemperatureService.wsdl'; $client = new SoapClient($wsdl_url); // New York, NY $temp = $client->getTemp('10001'); print $temp; 68
Reflection Series of classes to programmatically manipulate Classes Methods (and functions) Properties Parameters Extensions Useful for Class documenter Unit tester Debugger
Tidy “ Clean up” your HTML Smart HTML parser understands non-well-formed HTML Whips it into shape Ensures specification compliance Reduces bandwidth Useful for screen scraping
 

PHP 5 Boot Camp

  • 1.
    PHP 5 Bootcampfor PHP 4 Programmers Adam Trachtenberg eBay Technical Evangelist [email_address]
  • 2.
    Before we beginI know it’s early in the morning We’ll start slowly Ask lots of questions Don’t worry about interrupting Tell me to talk slower I’m from New York, so I talk too fast Enjoy!
  • 3.
  • 4.
    Tutorial Structure PHP5 is new, and so is this tutorial. Lots of information, so I will try and shape the lesson around your wants. Two primary objectives Highlight new features Tip you off towards incompatibilites Code samples use “Before & After” when applicable.
  • 5.
    Warm Up Whoknows PHP 4 PHP 5 OOP MySQL MySQL 4.1? XML DOM XPath
  • 6.
    Plan of AttackObject-Oriented Programming Databases MySQL SQLite XML DOM SimpleXML Everything else
  • 7.
    Object-Oriented Programming Groupfunctions and data together. Encourages encapsulation. And code reuse. Not so hot in PHP 4 Much better in PHP 5
  • 8.
  • 9.
    Object-Oriented Programming ReferencesConstructors & Destructors Visibility Interfaces & Abstract Classes Type Hints Static Methods & Properties Final Methods, Properties, & Classes Magical Methods
  • 10.
    Basic Concepts ClassProperty Method Object Instantiation (Constructors) Encapsulation Inheritance / Subclassing Extends Is a relationship Specialization
  • 11.
    References Objects inPHP5 passed by reference. A reference is a pointer to the variable. Any alterations made to the passed object are actually made to the original. Force pass by reference using “&”. Applies to assignments and function/method parameters.
  • 12.
    PHP 4 vsPHP 5 $rasmus = new Person; $rasmus->setName( 'Rasmus Lerdorf'); $zeev = $rasmus; $zeev->setName( 'Zeev Suraski'); print $rasmus-> getName(); print $zeev->getName(); Rasmus Lerdorf Zeev Suraski
  • 13.
    PHP 4 vsPHP 5 $rasmus = new Person; $rasmus->setName( 'Rasmus Lerdorf'); $zeev = $rasmus; $zeev->setName( 'Zeev Suraski'); print $rasmus-> getName(); print $zeev->getName(); Rasmus Lerdorf Zeev Suraski $rasmus = new Person; $rasmus->setName( 'Rasmus Lerdorf'); $zeev = $rasmus; $zeev->setName( 'Zeev Suraski'); print $rasmus-> getName(); print $zeev->getName(); Zeev Suraski Zeev Suraski
  • 14.
    PHP 5 vsPHP 5 w/clone $rasmus = new Person; $rasmus->setName( 'Rasmus Lerdorf'); $zeev = $rasmus; $zeev->setName( 'Zeev Suraski'); print $rasmus-> getName(); print $zeev->getName(); Zeev Suraski Zeev Suraski
  • 15.
    PHP 5 vsPHP 5 w/clone $rasmus = new Person; $rasmus->setName( 'Rasmus Lerdorf'); $zeev = $rasmus; $zeev->setName( 'Zeev Suraski'); print $rasmus-> getName(); print $zeev->getName(); Zeev Suraski Zeev Suraski $rasmus = new Person; $rasmus->setName( 'Rasmus Lerdorf'); $zeev = clone $rasmus; $zeev->setName( 'Zeev Suraski'); print $rasmus-> getName(); print $zeev->getName(); Rasmus Lerdorf Zeev Suraski
  • 16.
    Constructors Same asPHP 4, but new naming scheme __construct() Reduces brittleness Will revert to old PHP 4 style naming scheme
  • 17.
    PHP 4 vsPHP 5 class Database { function Database($h) { $this->h = db_connect($h); } } $db = new Database('db.example.com');
  • 18.
    PHP 4 vsPHP 5 class Database { function Database($h) { $this->h = db_connect($h); } } $db = new Database('db.example.com'); class Database { function __construct ($h) { $this->h = db_connect($h); } } $db = new Database('db.example.com');
  • 19.
    Destructors New inPHP 5 __destruct() Run whan last instance of object destroyed This is probably before the end of the request Much cooler than PHP 4 kludge using register_shutdown_function()
  • 20.
    PHP 4 vsPHP 5
  • 21.
    PHP 4 vsPHP 5 class Database { function __destruct() { db_close($this->h); } }
  • 22.
    Visibility Access restrictionson properties and methods Public: everyone (Replaces var ) Protected: class and descendents Private: class only Enforces encapsulation Distinction between public and private Public interface stays constant Private (internal) guts can be safely modifed at will
  • 23.
    PHP 4 vsPHP 5 class Person { var $name; function setName($name) { $this->name = $name; } } $rasmus = new Person; $rasmus->setName( 'Rasmus Lerdorf'); print $rasmus->name; Rasmus Lerdorf
  • 24.
    PHP 4 vsPHP 5 class Person { var $name; function setName($name) { $this->name = $name; } } $rasmus = new Person; $rasmus->setName( 'Rasmus Lerdorf'); print $rasmus->name; Rasmus Lerdorf class Person { private $name; public function setName($name) { $this->name = $name; } } $rasmus = new Person; $rasmus->setName( 'Rasmus Lerdorf'); print $rasmus->name; PHP Fatal error: Cannot access private property Person::$name
  • 25.
    Interfaces The mechanismfor forcing classes to support the same set of methods. Alternative to multiple inheritance. Used in Java (versus C++). Classes implement one or more interfaces. If a class promises to implement an interfaces, but doesn’t, it’s a fatal error. Pre-declare interfaces to keep PHP 5 happy.
  • 26.
    Sellable Interface interfaceSellable { public function getName(); public function getPrice(); } class Book implements Sellable { public function getName() { ... } public function getPrice() { ... } }
  • 27.
    Abstract Classes Similarto interfaces, but integrated into object heirarchy. Abstract base class, concrete children. Cannot be directly instantiated. The is a relationship applies.
  • 28.
    Abstract Database Class1/2 abstract class Database { abstract public function connect(); abstract public function close(); }
  • 29.
    Abstract Database Class2/2 class MySQL extends Database { protected $dbh; public function connect($s) { $this->dbh = mysql_connect($s); } public function close() { mysql_close($this->dbh); } }
  • 30.
    Type Hints Requireparameter must be of a certain class. Uses instanceof check. (Subclasses okay.) Only works for objects. Cannot pass NULL . Violations cause a fatal error.
  • 31.
    PHP 4 vsPHP 5 class AddressBook { function add($person) { if (!(is_a( $person, ‘Person’)) { die(&quot;Argument 1 must be an instance of Person&quot;); } // add $person to // address book } }
  • 32.
    PHP 4 vsPHP 5 class AddressBook { function add($person) { if (!(is_a( $person, ‘Person’)) { die(&quot;Argument 1 must be an instance of Person&quot;); } // add $person to // address book } } class AddressBook { public function add( Person $person) { // add $person to // address book } }
  • 33.
    Static Methods andProperties Static methods Callable without instantiation Use :: instead of -> Cannot refer to $this Static properties Class namespaced global properties Shared among all instances of a class Also use :: instead of -> Use self instead of $this
  • 34.
    Final Classes, Methods,and Properties Final Classes Prevent subclassing of entire object final class MySQL { … } Final Methods Prevent subclassing of individual method Even if all methods are final , can still subclass object final public function query($q) { … } Final Properties Object constants class Math { const pi = 3.14159; } $area = Math::pi * $radius * $radius;
  • 35.
    Magical Methods __construct()__destruct() __get() __set() __call() __toString() __clone()
  • 36.
    __get() and __set()Called when you try to read or write to an object property. Simplies property accessors. Screws up property inheritance. Only called for nonexistent properties. This applies to private and protected properties too!
  • 37.
    Accessors class Person{ private $data; public function __get($property) { return $this->data[$property]; } public function __set($property, $value) { $this->data[$property] = $value; } } $rasmus = new Person; $rasmus->name = 'Rasmus Lerdorf'; print $rasmus->name; Rasmus Lerdorf
  • 38.
    __call() Like __get() and __set() , but for methods. Captures any undefined methods. Used for object aggregation. And for dynamic proxy objects where the methods aren’t known ahead of time (like a SOAP client). Also screws up inheritance.
  • 39.
    __toString() Allows youto define a “pretty print” version of an object. Only called with print and echo . Won’t work with Concatenation ( . ) Casting ( strval() , (string) ) Functions that take strings Work around: explicitly call __toString() Should be “smarter” in PHP 5.1
  • 40.
    PHP 4 vsPHP 5 class Rasmus { function toString() { return “ Rasmus Lerdorf”; } } $rasmus = new Rasmus; print $rasmus-> toString(); Rasmus Lerdorf
  • 41.
    PHP 4 vsPHP 5 class Rasmus { function toString() { return “ Rasmus Lerdorf”; } } $rasmus = new Rasmus; print $rasmus-> toString(); Rasmus Lerdorf class Rasmus { public function __ toString() { return “ Rasmus Lerdorf”; } } $rasmus = new Rasmus; print $rasmus; Rasmus Lerdorf
  • 42.
    __clone() Called whenyou use clone operator. __clone() lets you override normal behavior. By default, PHP 5 does “shallow copy.” You may want “deep copy” or to otherwise control the cloning process. Given $this to play with.
  • 43.
    Databases MySQL NewMySQLi extension “ I” stands for “improved!” MySQL 4.1.2+ SQLite Database library Bundled with PHP 5 “ Flatfiles be gone!”
  • 44.
    MySQL Old MySQLextension showing its age. MySQL 4.1 introduced new client protocol. Time for a rewrite! Something old, something new… Some functions deleted Some functions changed Some functions added Porting isn’t s/mysql_/mysqli_/g;
  • 45.
    New Syntax Functionsbegin with mysqli_ No more default links. Database handle always comes first. mysqli_connect() has different prototype. No more persistant connections. Or mysql_escape_string() .
  • 46.
    MySQL vs MySQLi$db = mysql_connect( $server, $user, $pass); mysql_select_db( $db, &quot;users&quot;); $r = mysql_query( &quot;SELECT user FROM users&quot;, $db); while ($row = mysql_fetch_assoc($r)) { print $row['user']; } mysql_free_result($r); mysql_close($db);
  • 47.
    MySQL vs MySQLi$db = mysql_connect( $server, $user, $pass); mysql_select_db( $db, &quot;users&quot;); $r = mysql_query( &quot;SELECT user FROM users&quot;, $db); while ($row = mysql_fetch_assoc($r)) { print $row['user']; } mysql_free_result($r); mysql_close($db); $db = mysql i _connect( $server, $user, $password , &quot;users&quot; ); $r = mysql i _query( $db, &quot;SELECT user FROM users&quot;); while ($row = mysql i _fetch_assoc($r)) { print $row['user']; } mysql i _free_result($r); mysql i _close($db);
  • 48.
    OO Interface Firstclass database object PHP 5 feature Extendable Clearly separate multiple connections Cool. :)
  • 49.
    Procedural vs OO$db = mysqli_connect( $server, $user, $password, &quot;users&quot;); $r = mysqli_query( $db, &quot;SELECT user FROM users&quot;); while ($row = mysqli_fetch_assoc($r)) { print $row['user']; } mysqli_free_result($r); mysqli_close($db); $db = new mysqli ( $server, $user, $password, &quot;users&quot;); $r = $mysqli->query ( &quot;SELECT user FROM users&quot;); while ($row = $r->fetch_assoc ()) { print $row['user']; } $r->close(); unset($db);
  • 50.
    Prepared Statements Speedsup MySQL query processing. Query template with placeholders. Bind PHP variables into MySQL input. Bind MySQL ouput to PHP variables. Automatically escape quotes. Prevent security vulnerabilities.
  • 51.
    Traditional // User-entereddata $user = 'rasmus'; $pass = ’carl'; $zip = 94088; // Escape data $user = mysqli_real_escape_string($db, $user); $pass = mysqli_real_escape_string($db, $pass); $zip = intval($zip); // Create SQL query $sql = &quot;INSERT INTO users VALUES ('$user, '$pass, $zip)&quot;; // Make SQL query mysqli_query($db, $sql);
  • 52.
    Stored Procedure //User-entered data $user = 'rasmus'; $pass = 'carl'; $zip = 94088; // Prepare statement $sql = 'INSERT INTO users VALUES(?, ?, ?)'; $stmt = mysqli_stmt_init($db); if (mysqli_stmt_prepare($stmt, $sql)) { // Bind placeholders w/ types and var names // 'ssi’: two strings and one integer mysqli_stmt_bind_param($stmt, 'ssi', $user, $pass, $zip); // Execute statement mysqli_stmt_execute($stmt); }
  • 53.
    Bound Output Parameters$sql = 'SELECT username FROM users'; $stmt = mysqli_stmt_init($db); if (mysqli_stmt_prepare($stmt, $sql)) { // Bind result variables mysqli_stmt_bind_result($stmt, $username); mysqli_stmt_execute($stmt); // Place query data into bound result variables while (mysqli_stmt_fetch($stmt)) { print &quot;$username\n&quot;; } } ramsus zeev
  • 54.
    Subselects Embed onequery inside another Places logic inside MySQL instead of PHP Useful way of winnowing data Can often be avoiding by doing a self-join But are sometimes necessary
  • 55.
    Transactions Group relatedqueries into a single unit Database commits all or none Preserves consistency and integrity Useful in many cases Vital in banking when you can’t have two transactions cross in mid-stream
  • 56.
    Multi-Query Issue multiplequeries within a single call Only available with new MySQL 4.1 client protocol. Disabled for mysqli_query() Lots of work for minimal gain Only really useful if you’re writing something like phpMyAdmin
  • 57.
    SSL Encrypt communicationbetween PHP and MySQL using SSL Bit of a pain to set up if you’re unfamiliar with SSL Need to build PHP and MySQL with OpenSSL and relink PHP to SSL-enabled MySQL
  • 58.
    Migrating Should youmigrate? Harder then you think: PHP 4 to PHP 5 MySQL 3.x (or 4.0) to MySQL 4.1 MySQL to MySQLi Do it if you have to: MySQL 4.1 On going project
  • 59.
    Plan of AttackMove to MySQL 4.1.2 Move to PHP 5 Basic MySQLi Port Use MySQLi Specific Features
  • 60.
    SQLite Database ina box Always available Eliminates locking nightmares Faster searching Not actually “Lite” But it has some limitations
  • 61.
    Basics Databases arefiles Opening a database creates the file Typeless You can declare, but SQLite ignores (except for sorting). Not all that crazy. PHP is typeless and we like that, don’t we? Operates like most of DB extensions
  • 62.
    Example $db =sqlite_open('/www/support/users.db'); sqlite_query($db, 'CREATE TABLE users(username VARCHAR(100) password VARCHAR(100))'); $username = sqlite_escape_string($username); $password = sqlite_escape_string($password); sqlite_query($db, &quot;INSERT INTO users VALUES ('$username', '$password')&quot;); $r = sqlite_query($db, 'SELECT username FROM users'); while ($row = sqlite_fetch_array($r)) { // do something with $row }
  • 63.
    OO Interface $db= new SQLiteDatabase ('/www/support/users.db'); $db->query ( 'CREATE TABLE users(username VARCHAR(100), password VARCHAR(100))'); $username = sqlite_escape_string($username); $password = sqlite_escape_string($password); $db->query (&quot;INSERT INTO users VALUES ('$username', '$password')&quot;); $r = $db->query ('SELECT username FROM users'); while ($row = $r->fetch() ) { // do something with $row } unset($db);
  • 64.
    Iterators // oneat a time $r = $db->query('SELECT * FROM users'); foreach ($r as $row) { // do something with $row }
  • 65.
    In-Memory Tables Createan in-memory table by using the filename: :memory: Keeps the table in RAM for fast access. Doesn’t persist between requests. Best for one-time processing. Or when you need to make lots of queries.
  • 66.
  • 67.
    UDFs Write UDFsin PHP and register them with SQLite. Works for both standard and aggregate functions.
  • 68.
    XML Work togetheras a unified whole Are standardized on a single XML library: libxml2 Fully comply with W3 specifications Efficiently process data Provide you with the right XML tool for your job
  • 69.
    XML Extensions DOMXSLT SAX SimpleXML XPath XML Pull Parser (PECL)
  • 70.
    Document Object Model:DOM The 800-pound gorilla of XML. You can do everything and the <kitchen-sink> with DOM. Navigating through documents can be cumbersome. Tree-based API, must load entire document into memory. Much better than PHP 4 DOMXML extension, but not backwards compatible.
  • 71.
    XSLT Transform XMLdocuments into HTML XML (RSS, SOAP, etc.) Anything else Uses XML-based stylesheets. Easily shared among different applications. Has a quirky syntax. Uses libxslt instead of Sablotron.
  • 72.
    Simple API forXML: SAX PHP’s original XML extension. Streaming, or event-based, parser Uses less memory than DOM, but frequently requires more complex PHP code. Now uses libxml2 instead of expat. Translation layer provides compatability. Almost does a good job. :)
  • 73.
    SimpleXML New PHP5–only extension Excels at parsing RSS files, REST results, and configuration data. If you know the document’s format ahead of time, SimpleXML is the way to go. However, SimpleXML supports only a subset of the XML specification. If you only remember one XML topic from this talk, it should be SimpleXML.
  • 74.
    XPath Regular expressionsfor XML documents. Isolate the data you want. Used in XSLT. Also availble in DOM and SimpleXML If you only remember two XML topics from this talk, it should be SimpleXML and XPath.
  • 75.
    Reading: DOM: PHP4 $dom = domxml_open_file('address-book.xml'); foreach ($dom->get_elements_by_tagname('person') as $person) { $firstname = $person-> get_elements_by_tagname('firstname'); $firstname_text = $firstname[0]->first_child(); $firstname_text_value = $firstname_text-> node_value(); print &quot;$firstname_text_value\n&quot;; }
  • 76.
    Reading: DOM: PHP5 $dom = new DOMDocument; $dom->load('address-book.xml'); foreach ($dom->getElementsByTagname('person') as $person) { $firstname = $person-> getElementsByTagname('firstname'); $firstname_text_value = $firstname->item(0)-> firstChild->nodeValue; print &quot;$firstname_text_value\n&quot;; }
  • 77.
    What’s different? studlyCapsget_elements_by_tagname() getElementsByTagname() DOM node list vs array $firstname[0] $firstname->item(0) Methods vs Properties first_child() firstChild Object vs Resource
  • 78.
    SimpleXML $sx =simplexml_load_file( 'address-book.xml'); foreach ($sx->person as $person) { print $person->firstname \n&quot;; }
  • 79.
    Searching: PHP 4$dom = domxml_open_file( 'address-book.xml'); $xpath = xpath_new_context($dom); $emails = $xpath->xpath_eval( '/address-book/person/email'); foreach ($emails->nodeset as $e) { $tmp = $e->first_child(); $email = $tmp->node_value(); // do something with $email }
  • 80.
    Searching: PHP 5& XPath $dom = new DOMDocument; $dom->load('address-book.xml'); $xpath = new DOMXPath($dom); $emails = $xpath->query( '/address-book/person/email'); foreach ($emails as $e) { $email = $e->firstChild-> nodeValue; // do something with $email }
  • 81.
    Searching: PHP 5& SimpleXML $s = simplexml_load_file( 'address-book.xml'); $emails = $s->xpath( '/address-book/person/email'); foreach ($emails as $email) { // do something with $email }
  • 82.
    Transforming Pass DOMobjects Nicer, cleaner, faster libxslt instead of Sablotron
  • 83.
    PHP 4 vsPHP 5 $xml = 'data.xml'; $xsl = 'style.xsl'; $xslt = xslt_create(); $results = xslt_process($xslt, $xml, $xsl); xslt_free($xslt);
  • 84.
    PHP 4 vsPHP 5 $xml = 'data.xml'; $xsl = 'style.xsl'; $xslt = xslt_create(); $results = xslt_process($xslt, $xml, $xsl); xslt_free($xslt); $xml = new DOMDocument; $xml->load('data.xml'); $xsl = new DOMDocument; $xsl-> load('style.xsl'); $xslt = new XSLTProcessor(); $xslt-> importStylesheet($xsl); $results = $xslt-> transformToXML($xml);
  • 85.
    Everything else Iteratorsand SPL Streams Exceptions SOAP Reflection Tidy
  • 86.
    Iterators Allow youto loop through objects using foreach() in a controlled fashion. Encapsulate looping logic inside class. Reduces errors More powerful behaviors SPL provides some generic classes. And some examples.
  • 87.
    Functions vs Iterators$d = opendir($path); while ( $file = readdir($d)) { print &quot;$file\n&quot;; } closedir($d);
  • 88.
    Functions vs Iterators:Take 2 $d = opendir($path); while ( false !== ($file=readdir($d))) { print &quot;$file\n&quot;; } closedir($d);
  • 89.
    Functions vs Iterators$d = opendir($path); while ( false !== ($file=readdir($d))) { print &quot;$file\n&quot;; } closedir($d); foreach ( new DirectoryIterator( $path) as $file) { print &quot;$file\n&quot;; }
  • 90.
    Streams, Wrappers, andFilters Place file interface upon protocols Allow fopen() and friends to “speak” Files http ftp Available in PHP 4.3, but not widely used PHP 5 offers new and improved version Can implement custom wrappers and filters in C and PHP
  • 91.
    Exceptions New wayof error handling Instead of checking return values, you “catch” error objects. Used in Java Can be used for good… and for evil! PHP 5 provides complete framework, but not well-baked into core, key extensions.
  • 92.
    PHP 4 $version= '1.0'; $element = 'address-book'; $dom = domxml_new_doc($version); $ab = $dom->create_element($element); $ab = $dom->append_child($ab);
  • 93.
    PHP 4 w/ErrorChecking $version = '1.0'; $element = 'address-book'; if ($dom = domxml_new_doc($version)) { if ($ab = $dom->create_element($element)) { if ($ab = $dom->append_child($ab)) { // Successfully appended address-book element } else { // Cannot append child } } else { // Cannot create <address-book> element } } else { // Cannot create new document }
  • 94.
    PHP 5 w/Exceptions$version = '1.0'; $element = 'address-book'; try { $dom = new DOMDocument($version); $ab = new DOMElement($element); $ab = $dom->appendChild($ab); } catch (DOMException $e) { print $e; }
  • 95.
    Exception Class Populatedw/data by PHP about the error Message, Code, Line, File, Backtrace __toString() produces formated message
  • 96.
    SOAP Key componentof Web services PHP 4 only offers PHP-based packages PHP 5 bundles C-based extension! Not enabled by default Uses libxml2 SOAP works hard to make things easy. Semi-officially sponsored by Zend. SOAPClient and SOAPServer
  • 97.
    SOAPClient Request $wsdl_url= 'http://www.xmethods.net/sd/2001/'. 'TemperatureService.wsdl'; $client = new SoapClient($wsdl_url); // New York, NY $temp = $client->getTemp('10001'); print $temp; 68
  • 98.
    Reflection Series ofclasses to programmatically manipulate Classes Methods (and functions) Properties Parameters Extensions Useful for Class documenter Unit tester Debugger
  • 99.
    Tidy “ Cleanup” your HTML Smart HTML parser understands non-well-formed HTML Whips it into shape Ensures specification compliance Reduces bandwidth Useful for screen scraping
  • 100.