PHP: using __autoload() for automatically loading php files

30 May

Object oriented programming give great strength to PHP. Now if you need to work on large web application, it’ll be nice to identify entities and construct separate class for each.

Mostly the entities identified in the analysis phase need a separate database tables. So it would be better to define a separate class for each of your database table. The class created encapsulates attributes and methods.

Give it a try, and honestly you will feel lot of strength of object oriented programming.

For example, we have a database table named users, having id, username, email and address.

You can define your table for this table as

<?php

class Users

{

private $_id;

private $_username;

private $_email;

public function __construct()

{

// code for connecting to the database

}

public save($data)

{

// code for saving user data.

}

}

Well, this isn’t a perfect definition of the class, however you can get an idea from this and construct your own according to your needs/requirements.

Large web application often need lots of require/require_once statements.

If you define a separate class for each of you business entity and need multiple classes in a single page, you will need to write as many require statements as the classes you need.

If your application page needs five php files, you will need to write five require statements.

The people behind PHP has given a very simple solution to get rid of this. You need to define a simple __autoload() method in the page, which will load all the class you need using a single require statement.

Let have a simple example.

We have two separate class for person and car.

The person class is define as

<?php

class Person

{

private $_name;

public function setName($n)

{

$this->_name = $n;

}

public function getName()

{

return $this->_name;

}

}

And class Car is defined as

<?

class Car

{

private $_made;

public function setMade($m)

{

$this->_made = $m;

}

public function getMade()

{

return $this->_made;

}

}

Now if you need both these classes in the same file, you can either user two require statements as

require_once(‘person.php’);

require_once(‘car.php’);

And then create objects of each of these classes and call appropriate methods.

$person = new Person();

$car = new Car();

$person->setName(‘faheem’);

$car->setModel();

Although its simple to have two require statements, however if you need more, it becomes painful.

If you remove these two require statements, you will face “Class not found” fetal error.

Using __autoload() method you can get rid of these or as many requirement statements as you require.

Define the following function in the file where you create objects of different classes as

function __autoload($class) {

require_once($class.’.php’);

}

let we have a file named index.php where we need these classes. So our index.php will look like this.

<?

function __autoload($class)

{

require_once($class.’.php’);

}

$person = new Person();

$car = new Car();

$person->setName(‘faheem’);

$car->setModel();

?>

No matter, where you define this method, at the beginning or at the end, result will be the same.

Give it a try and let me know if you face any problem.

One Response to “PHP: using __autoload() for automatically loading php files”

  1. cevarief May 31, 2009 at 1:43 am #

    It’s simple and nice. Thx.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: