Autoload function in PHP5

AID: 676
  • Status: Published

1760 points

  • Bystaarland
  • TypeTips/Tricks
  • Posted on2009-05-22 at 05:09:54

As you all know PHP got one problem, before using other files classes or functions you need to include or use require function. But now in PHP 5 there's solution for this, it's called __autoload() function. You can read more about function in PHP manual http://ee.php.net/manual/en/function.spl-autoload.php.

What would be best way to use this, as you have seen the usage in Zend Framework, they have got really simple way to do this. All the class names contain the path in names. Like it would be: My_Database_Mysql so this means it's located in folder My/Database/Mysql.php. You could use your own delimiter (- . :).

As the zip with php files isn't allowed to upload so explain the scheme of directories.
/library/Database/Mysql/Pdo.php
/library/Database/Mysql/Mysqli.php
/library/Database/Mysql/Default.php

/library/Database/Oracle/Default.php

<?php
set_include_path('./library');

function __autoload($className) {
	$path = str_replace('_', '/', $className);
	require_once("{$path}.php");
}
$pdo = new Database_Mysql_Pdo();
var_dump($pdo);

$mysqli = new Database_Mysql_Mysqli();
var_dump($mysqli);
?>
                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

Select allOpen in new window

Asked On
2009-05-22 at 05:09:54ID676
Tags

php

,

autoload

,

include

,

require

,

big projects

Topic

PHP Scripting Language

Views
1246

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Loading Advertisement...

Top PHP Experts

  1. Ray_Paseur

    326,882

    Wizard

    4,070 points yesterday

    Profile
    Rank: Savant
  2. Roads_Roads

    77,834

    Master

    0 points yesterday

    Profile
    Rank: Genius
  3. maeltar

    71,332

    Master

    0 points yesterday

    Profile
    Rank: Guru
  4. StingRaY

    70,054

    Master

    0 points yesterday

    Profile
    Rank: Wizard
  5. DaveBaldwin

    64,155

    Master

    664 points yesterday

    Profile
    Rank: Genius
  6. jason1178

    37,050

    0 points yesterday

    Profile
    Rank: Genius
  7. COBOLdinosaur

    30,996

    664 points yesterday

    Profile
    Rank: Genius
  8. xterm

    28,850

    0 points yesterday

    Profile
    Rank: Sage
  9. eriksmtka

    27,641

    0 points yesterday

    Profile
    Rank: Master
  10. smadeira

    26,150

    0 points yesterday

    Profile
    Rank: Guru
  11. webmatrixpune

    23,436

    0 points yesterday

    Profile
    Rank: Guru
  12. logudotcom

    19,598

    10 points yesterday

    Profile
    Rank: Genius
  13. bportlock

    17,470

    0 points yesterday

    Profile
    Rank: Genius
  14. Derokorian

    17,368

    0 points yesterday

    Profile
    Rank: Guru
  15. maestropsm

    16,698

    0 points yesterday

    Profile
    Rank: Master
  16. leakim971

    16,600

    0 points yesterday

    Profile
    Rank: Genius
  17. alex_code

    16,402

    0 points yesterday

    Profile
    Rank: Guru
  18. mwvisa1

    14,400

    0 points yesterday

    Profile
    Rank: Genius
  19. hernst42

    14,332

    0 points yesterday

    Profile
    Rank: Genius
  20. pratima_mcs

    14,200

    0 points yesterday

    Profile
    Rank: Genius
  21. Slick812

    13,900

    0 points yesterday

    Profile
    Rank: Sage
  22. elvin66

    12,628

    0 points yesterday

    Profile
    Rank: Wizard
  23. zappafan2k2

    12,200

    0 points yesterday

    Profile
    Rank: Guru
  24. TerryAtOpus

    11,600

    0 points yesterday

    Profile
    Rank: Genius
  25. amar_bardoliwala

    11,500

    0 points yesterday

    Profile
    Rank: Master

Hall Of Fame