Saturday, October 29, 2011

Difference between PHP 4 and 5 - Magic Methods

There are a number of "magic methods" that add an assortment to functionality to your classes. Note that PHP reserves the naming of methods prefixed with a double-underscore. Never name any of your methods with this naming scheme!

Some magic methods to take note of are __call, __get, __set and __toString. These are the ones I find most useful.

Difference between PHP 4 and 5 - __autoload Function

Using a specially named function, __autoload, you can automatically load object files when PHP encounters a class that hasn't been defined yet. Instead of large chunks of include's at the top of your scripts, you can define a simple autoload function to include them automatically.

function __autoload($class_name) {
require_once "./includes/classes/$";

Note you can change the autoload function or even add multiple autoload functions using spl_autoload_register and related functions.

Difference between PHP 4 and 5-New Extensions

PHP5 also introduces new default extensions.

1. SimpleXML for easy processing of XML data
2. DOM and XSL extensions are available for a much improved XML-consuming experience. A breath of fresh air after using DOMXML for PHP4!
3. PDO for working with databases. An excellent OO interface for interacting with your database.
4. Hash gives you access to a ton of hash functions if you need more then the usual md5 or sha1.