Title: Migration to PHP 5'1
1Migration to PHP 5.1
Davey Shafik
davey_at_php.net
2About Me
- Davey Shafik is
- A long time contributor to PEAR
- http//pear.php.net
- A long time blogger
- http//pixelated-dreams.com
- A long time helper
- irc.freenode.net PHP php.thinktank
- Contributor to the Zend PHP Framework
- http//framework.zend.com
- Hard of hearing, so speak up!
3This Session
- We will be covering
- Migrating the Developer
- From PHP 4.x
- From PHP 5.0
- Migrating the Code
- Pratical Examples
- New Stuff
- Warning Lots of Object Orientated Code Follows.
4Questions?
Feel free to interrupt me!
5Migrating the Developer
6I Want THAT One!
- References are one of the few changes that
effects procedural code. - All objects are now returned as references not
copies. - You must now return a variable if returning by
references. - Foreach can use References
- Referenced arguments can now have a default value
- New clone keyword to unreference objects.
7Object References
- lt?phpobj new stdClassobj-gtfoo 'bar'o
bj2 objobj2-gtfoo 'qux'obj3 clone o
bjobj3-gtfoo 'bat'var_dump(obj, obj2, o
bj3)?gt
Output object(stdClass)1 (1) "foo"gt
string(3) "qux" object(stdClass)1 (1)
"foo"gt string(3) "qux" object(stdClass)2
(1) "foo"gt string(3) "bat"
8Return by Reference
- lt?phpfunction foo() return nullfoo()
?gt
Notice Only variable references should be
returned by reference in filename on line 5
9Foreach Using References
- lt?phparray array(2,4,6,8,10)foreach (arra
y as value) value - 1var_dump(arra
y)?gt
Output array(5) 0gt int(1) 1gt
int(3) 2gt int(5) 3gt int(7)
4gt int(9)
10Reference Argument Defaults
- lt?phpfunction foo(bar 'baz') echo bar
foo()?gt
Output bar
11Build It and then Break It
- Unified __construct() method no longer named
after the class. - The PHP 4 way still works.
- parent__construct() still not called.
- Unified __destruct() method now you can
shutdown your objects.
12Constructors Destructors
- lt?phpclass foo public function __construc
t() echo __METHOD__ . PHP_EOL
public function __destruct() e
cho __METHOD__ foo new foo()?gt
Output foo__construct foo__destruct
13Yours, Mine and Everybodys
- PHP 5.0 supports Class method and member
visibility. - Public Anyone can use them
- Protected Only the class and sub-classes can
use them - Private Only the class can use them
- Final Subclasses cannot override it, or in the
case of a final class, cannot extend it.
14Public, Private, Protected
- lt?php class foo public foo 'bar'
protected baz 'bat' private qux '
bingo' function __construct()
var_dump(get_object_vars(this))
class bar extends foo function __const
ruct() var_dump(get_object_vars(this))
class baz function __construc
t() foo new foo var_dump(g
et_object_vars(foo))
new foo new bar new baz - ?gt
Output array(3) "foo"gt string(3) "bar"
"baz"gt string(3) "bat" "qux"gt
string(5) "bingo" array(2) "foo"gt
string(3) "bar" "baz"gt string(3)
"bat" array(1) "foo"gt string(3) "bar"
15Static Shock
- PHP 5 introduces static properties/methods and
class constants. - Static Properties Methods are
- Accessible class-wide
- Attached to the class
- Called using the operator
- Class Constants are
- Immutable just like standard constants
- Accessible class-wide
- Attached to the class
16Static Properties
- lt?phpclass foo public static bar "baz"
private static bat "qux" public
static function getBat() return selfb
at echo foobar . PHP_EOLecho foog
etBat() . PHP_EOLecho foobat . PHP_EOL?gt
Output baz qux Fatal error Cannot access
private property foobat in file on line 14
17Class Constants
- lt?phpclass foo const BAR 'bat' cons
t BAZ 'qux'echo fooBARecho fooBAZ?
gt
Output batqux
18Round Peg, Round Hole
- With PHP 5 comes the introduction of Interfaces
and Abstract classes - Interfaces define rigid APIs
- Interfaces define API only.
- Your Class must conform to the API
- Abstract classes define partial classes
- Cannot be instantiated themselves
- Must be extended
- May contain method bodies
- Your class must conform to the API
19Interfaces
- lt?php interface DBAdapter public function
connect(user, pass) public function
query(sql, options) public function
getRow() public function getAll()
public function close()
class foo implements DBAdapter ?gt
Fatal error Class foo contains 5 abstract
methods and must therefore be declared abstract
or implement the remaining methods
(DBAdapterconnect, DBAdapterquery,
DBAdaptergetRow, ...) in file on line 15
20Interfaces
- lt?php interface DBAdapter public function
connect(user, pass) public function
query(sql, options)
class foo implements DBAdapter - public function connect(user, pass)
- public function query(sql)
- ?gt
Fatal error Declaration of fooquery() must be
compatible with that of DBAdapterquery() file
on line 9
21Extending Interfaces
- lt?phpinterface DBAdapter public function co
nnect(user, pass) public function quer
y(sql, options) public function getRow
() public function getAll() pu
blic function close()interface MysqlDBAdapter
extends DBAdapter public function escape(s
tring) - ?gt
22Abstract Classes
- lt?phpabstract class foo new foo?gt
Fatal error Cannot instantiate abstract class
foo in file on line 6
23Abstract Classes (cont.)
- lt?phpabstract class foo function bar()
class baz extends foo abstract class bar
abstract function bat()class qux extends
bar ?gt
function bat()
Fatal error Class qux contains 1 abstract method
and must therefore be declared abstract or
implement the remaining methods (barbat) in
file on line 14
24Who's Your Daddy?
- Classes can still only inherit from one class.
- Classes can implement multiple interfaces.
- Parent constructors/destructors not called.
- New self keyword added, works like parent but
references the current class.
25Multiple Interfaces
- lt?phpinterface foo interface bar class
MyClass implements foo,bar ?gt
26Multiple Interfaces (Cont.)
- lt?phpinterface foo function doFoo()
interface bar function doBar()class
MyClass implements foo,bar function doFoo()
?gt
Fatal error Class MyClass contains 1 abstract
method and must therefore be declared abstract or
implement the remaining methods (bardoBar) in
file on line 16
27Migrating The Code
28And... wakeup!
- Insert Exciting and Witty Comment Here
29Introducing XML. Again.
- XML is considered one of the largest changes
between PHP 4 and PHP 5. - All extensions now based on libxml2
- New DOM extension W3C Compliant
- New XSLT extension Supports EXSLT and User
Defined Functions - New SimpleXML extension super easy XML reading
- New XMLReader and XMLWriter extensions
- Expat extension deprecated
- DOMXML and Sablotron no longer supported
30Example 1 RSS Feed
- lt?xml version"1.0" encoding"utf-8" ?gt
- ltrss version"0.91" gt
- ltchannelgt
- lttitlegtPixelated Dreamslt/titlegt
- ltlinkgthttp//pixelated-dreams.com/lt/linkgt
- ltdescriptiongtAs close to my brain as you can
safely getlt/descriptiongt - ltlanguagegtenlt/languagegt
- ltimagegt
- lturlgthttp//pixelated-dreams.com/templates/defa
ult/img/s9y_banner_small.pnglt/urlgt - lttitlegtRSS Pixelated Dreams - As close
to my brain as you can safely getlt/titlegt - ltlinkgthttp//pixelated-dreams.com/lt
/linkgt - ltwidthgt100lt/widthgt
- ltheightgt21lt/heightgt
- lt/imagegt
- ltitemgt
- lttitlegtphptek Day 1lt/titlegt
- ltlinkgthttp//pixelated-dreams.com/archives/226-
phptek-Day-1.htmllt/linkgt - ltdescriptiongt
- POST CONTENT
31Example 1 Output
- Pixelated Dreams
- phptek Day 1
- I gave my "Future Deployment of PHP Applications"
talk today. Unfortunately, I had quite a few
technical issues with the internet and such,
meaning I had to scrabble around to get the end
result working - but, I did manage it, though a
little sloppily still, it was my first time and
I'm looking forward to tomorrow with gusto. - My talk tomorrow is on "Migration to PHP 5.1" and
due to having no "live" examples, should go
without a hitch. - Other than preparing for my own talk, and taking
the afternoon off to get over it, I haven't
attended much. I got some good feedback on my
talk - and I promised to post my slides and
links, so I will do that shortly - To everybody that attended, thank you.
- I did video the talk, but have a break in the
continuity of about 5 or so minutes, so I'm going
to see what I can do about filling in that blank
and producing a Flash movie with the video and
slides in sync - it could be a while - - Davey
32Databases. All of them.
- Another great addition to PHP 5, is the PHP Data
Objects extension PDO. - PDO allows interation with many different RDBMSs
- PDO is
- bundled with PHP 5.1
- available as a PECL extensin for PHP 5.0
- not an abstraction layer. Kinda.
33PDO
- PDO Currently Supports
- MySQL
- PostgreSQL
- SQLite
- Oracle
- Firebird
- Informix
- DBLIB
- ODBC
34Why Using Databases Sucks
- MySQL
- mysql_connect(host, user, pass)
- MySQLi
- new mysqli(host, user, pass)
- PostgreSQL
- pg_connect(hosthost portport dbnamedb)
- SQLite
- new sqlite_database(db.sqlite)
35Why Things Dont Suck Anymore
- lt?php dsn "mysqlhosthost
usernameuser password
pass" // or dsn "pgsqlhosthost
portport dbnamedb
useruser passwordpa
ss" // or dsn "sqlite/opt/db.sqlite"
pdo new PDO(dsn, user, pass) ?gt
36Realistic Examples? Yeah. Right.
- lt?phppdo new PDO("mysqlhostlocalhostdbname
pear", "pear", "pear")stmt pdo-gtprepare( - "INSERT INTO table ( username, password , is_admin
) VALUES ( username, password, isadmin ) - )values'username' _POST'username'va
lues'password' _POST'password'values'
isadmin' _POST'is_admin'stmt-gtexecute(v
alues)?gt
37New Goodies
38Ooooh Shiny
- PHP 5.0 and 5.1 also bring many things to the
table that never existed before. - Object Overloading
- Properties
- Methods
- Type hints
- For objects and arrays
- Iterators
- The Standard PHP Library SPL
- Provides Exciting new capabilities for objects
39Object Overloading
- Overloading Setting Properties
- function __set(name, value)
- Overloading Getting Properties
- function __get(name)
- Overloading Methods
- function __call(method, args)
- Overloading Isset and Unset
- function __isset(name)
- function __unset(name)
40Type Hints
- PHP 5 introduces object type hints. With PHP 5.1
we also have Array type hints. - lt?phpfunction require_class (stdClass obj) r
equire_class("foo")?gt -
- lt?phpfunction require_array(Array array) req
uire_array("bar")?gt
Fatal error Argument 1 passed to
require_class() must be an object of class
stdClass, called in file on line 3 and defined
in file on line 2
Fatal error Argument 1 passed to require_array()
must be an array, called in file on line 9 and
defined in file on line 8
41Iterators
- Iterators are one of the most advanced new
features added to PHP 5. Iterators deserve a talk
to themselves. - Iterators allow
- Simple Iteration over a set of data
- Recursive Iteration
- Filtered Iteration
- Seekable Iteration
- Aggregated Iterations
42Simple Iterators
- A simple iterator implements the Iterator
interface and allows iteration over a set of data
lt?phpinterface Iterator function current()
function next() function rewin
d() function key() function va
lid()?gt
43Seekable Iterators
- Seekable iterators add the ability to move to a
specific key.
lt?phpinterface SeekableIterator function cu
rrent() function next() functi
on rewind() function key() fun
ction valid() function seek(index)?
gt
44Aggregated Iteration
- lt?phpinterface IteratorAggregate function g
etIterator()?gt
45Meet, Super Object
lt?phpinterface ArrayAccess function offsetE
xists(offset) function offsetGet(offse
t) function offsetSet(offset, value)
function offsetUnset(offset)?gt