Home arrow PHP arrow Page 2 - The Autoload Magic Function in PHP 5

Review: the destruct method - PHP

Among the improvements and new features that were introduced to PHP 5, thereís a set of special functions, popularly known as magic functions. These allow you to perform all sorts of smart tasks, ranging from overloading properties and methods in classes, to using destructors and triggering automatically predefined processes when serializing and unserializing objects. This is the conclusion to a seven-part series that shows you how to use the magic functions in PHP 5.

TABLE OF CONTENTS:
  1. The Autoload Magic Function in PHP 5
  2. Review: the destruct method
  3. Loading classes automatically with the autoload magic function
  4. Loading classes in the background with the autoload function
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
June 22, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In case you havenít had the chance to read the preceding article of this series, where I explained how to implement a simple destructor inside a class to create objects that are capable of persisting across several HTTP requests, below I reintroduced the full source code of this class, accompanied by a short example that shows how to use it.

Hereís the corresponding code sample:

// define 'User' class (implements the __destruct() magic method)

class User

{

// constructor (not implemented)

public function _construct(){}

 

// set undeclared property in a restrictive way

public function __set($property, $value)

{

if (in_array($property, array('fname', 'lname', 'email')) === TRUE)

{

$this->$property = $value;

}

}

 

// get declared property

public function __get($property)

{

if (isset($this->$property))

{

return $this->$property;

}

}

 

// single point to fetch user data

public function __call($method, $args)

{

if ($method === 'fetch' AND empty($args) === FALSE)

{

return $this->$args[0];

}

}

public function __destruct()

{

session_start();

$_SESSION['user'] = serialize($this);

}

}

Apart from overloading properties and methods via the ď__set()Ē, ď__get()Ē and ď__call()Ē functions, the above class also gives a simple implementation for its destructor, which saves an instance of any user object to a session variable right before being destroyed by the PHP interpreter.

Of course, the previous code sample would be incomplete if I donít create a script that shows how the destructor does its business. Here it is:

// example on implementing the '__destruct()' magic method

$user = new User();

$user->fname = 'Alejandro';

$user->lname = 'Gervasio';

$user->email = 'alejandro@mydomain.com';

Though it's a little bit rudimentary, this example depicts pretty clearly how to use the ď__destruct()Ē method to create classes that can save themselves to a predefined storage mechanism, in this case a session variable. However, itís possible to use a database or even a flat file to produce a similar result.

Now that youíre pretty familiar with implementing destructors, itís time to explore another magic function provided by PHP 5. So, in accordance with the concepts I expressed at the beginning, the last function that Iím going to cover in this series will be the popular ď__autoload(),Ē which can be really useful for loading classes without having to code any PHP include statements explicitly.

The implementation and use of this function will be discussed in detail in the following section. Thus, to learn more, please click on the link shown below and keep reading.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: