php 5 boot camp
DESCRIPTION
TRANSCRIPT
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
PHP 5 Bootcampfor PHP 4 Programmers
Adam Trachtenberg
eBay Technical Evangelist
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Before we begin
1. I know it’s early in the morning– We’ll start slowly
2. Ask lots of questions– Don’t worry about interrupting
3. Tell me to talk slower– I’m from New York, so I talk too fast
4. Enjoy!
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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.
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Warm Up
• Who knows– PHP 4– PHP 5– OOP– MySQL
• MySQL 4.1?
– XML• DOM• XPath
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Plan of Attack
• Object-Oriented Programming• Databases
– MySQL– SQLite
• XML– DOM– SimpleXML
• Everything else
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Object-Oriented Programming
• Group functions and data together.
• Encourages encapsulation.
• And code reuse.
• Not so hot in PHP 4
• Much better in PHP 5
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Object-Oriented Programming
• References• Constructors
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Object-Oriented Programming
• References• Constructors & Destructors• Visibility• Interfaces & Abstract Classes• Type Hints• Static Methods & Properties• Final Methods, Properties, & Classes• Magical Methods
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Basic Concepts
• Class– Property– Method
• Object– Instantiation (Constructors)– Encapsulation
• Inheritance / Subclassing– Extends– Is a relationship– Specialization
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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.
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Constructors
• Same as PHP 4, but new naming scheme• __construct()• Reduces brittleness
• Will revert to old PHP 4 style naming scheme
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
PHP 4 vs PHP 5
class Database {
function Database($h) {
$this->h = db_connect($h);
}
}
$db = new
Database('db.example.com');
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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');
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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()
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
PHP 4 vs PHP 5
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
PHP 4 vs PHP 5
class Database {
function __destruct() {
db_close($this->h);
}
}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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.
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Sellable Interface
interface Sellable {
public function getName();
public function getPrice();
}
class Book implements Sellable {
public function getName() { ... }
public function getPrice() { ... }
}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Abstract Classes
• Similar to interfaces, but integrated into object heirarchy.
• Abstract base class, concrete children.
• Cannot be directly instantiated.
• The is a relationship applies.
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Abstract Database Class 1/2
abstract class Database {
abstract public function connect();
abstract public function close();
}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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);}
}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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.
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
}
}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
}
}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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;
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Magical Methods
• __construct()• __destruct()• __get()• __set()• __call()• __toString()• __clone()
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
__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!
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
__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.
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
__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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
PHP 4 vs PHP 5
class Rasmus {
function toString() {
return
“Rasmus Lerdorf”;
}
}
$rasmus = new Rasmus;
print $rasmus->
toString();
Rasmus Lerdorf
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
__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.
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Databases
• MySQL– New MySQLi extension– “I” stands for “improved!”– MySQL 4.1.2+
• SQLite– Database library– Bundled with PHP 5– “Flatfiles be gone!”
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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;
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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().
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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);
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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 = 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);
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
OO Interface
• First class database object
• PHP 5 feature
• Extendable
• Clearly separate multiple connections
• Cool. :)
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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);
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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.
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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);
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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 integermysqli_stmt_bind_param($stmt, 'ssi',
$user, $pass, $zip);// Execute statement mysqli_stmt_execute($stmt);
}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Bound Output Parameters
$sql = 'SELECT username FROM users';$stmt = mysqli_stmt_init($db);if (mysqli_stmt_prepare($stmt, $sql)) {
// Bind result variablesmysqli_stmt_bind_result($stmt, $username);mysqli_stmt_execute($stmt);// Place query data into bound result variableswhile (mysqli_stmt_fetch($stmt)) { print "$username\n";}
}ramsuszeev
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Plan of Attack
1. Move to MySQL 4.1.2
2. Move to PHP 5
3. Basic MySQLi Port
4. Use MySQLi Specific Features
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
SQLite
• Database in a box
• Always available
• Eliminates locking nightmares
• Faster searching
• Not actually “Lite”
• But it has some limitations
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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);
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Iterators
// one at a time
$r = $db->query('SELECT * FROM users');
foreach ($r as $row) {
// do something with $row
}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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.
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Transactions
• SQLite supports transactions
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
UDFs
• Write UDFs in PHP and register them with SQLite.
• Works for both standard and aggregate functions.
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
XML Extensions
• DOM
• XSLT
• SAX
• SimpleXML
• XPath
• XML Pull Parser (PECL)
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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.
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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.
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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. :)
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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.
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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.
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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 "$firstname_text_value\n";
}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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 "$firstname_text_value\n";
}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
SimpleXML
$sx = simplexml_load_file(
'address-book.xml');
foreach ($sx->person as $person) {
print $person->firstname \n";
}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Transforming
• Pass DOM objects
• Nicer, cleaner, faster• libxslt instead of Sablotron
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
PHP 4 vs PHP 5
$xml = 'data.xml';
$xsl = 'style.xsl';
$xslt =
xslt_create();
$results =
xslt_process($xslt,
$xml, $xsl);
xslt_free($xslt);
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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);
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Everything else
• Iterators and SPL
• Streams
• Exceptions
• SOAP
• Reflection
• Tidy
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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.
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Functions vs Iterators
$d = opendir($path);
while (
$file = readdir($d)) {
print "$file\n";
}
closedir($d);
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Functions vs Iterators: Take 2
$d = opendir($path);
while (
false !==
($file=readdir($d))) {
print "$file\n";
}
closedir($d);
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Functions vs Iterators
$d = opendir($path);
while (
false !==
($file=readdir($d))) {
print "$file\n";
}
closedir($d);
foreach (
new DirectoryIterator(
$path) as $file) {
print "$file\n";
}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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.
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
PHP 4
$version = '1.0';
$element = 'address-book';
$dom = domxml_new_doc($version);
$ab = $dom->create_element($element);
$ab = $dom->append_child($ab);
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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;}
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Exception Class
• Populated w/data by PHP about the error
• Message, Code, Line, File, Backtrace• __toString() produces formated
message
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
Reflection
• Series of classes to programmatically manipulate– Classes– Methods (and functions)– Properties– Parameters– Extensions
• Useful for– Class documenter– Unit tester– Debugger
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers
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
Adam TrachtenbergPHP 4 Bootcamp for PHP 5 Programmers