Victor's Blog about PHP, Zend Framework & Cake PHP

This blog is about PHP in general. It tackles various topics related to the language itself especially at the OOP level, discusses various topics related to Zend Framework and shares my experience with Cake PHP. I believe that mastering technology is heavily based on one's ability to discuss its details and share knowledge with others. Technology is a wide wild world after all!

PHP Autoloading

Printable Version

victor | 22 February, 2010 09:20

Autoloading is the process of automatically loading classes when needed wthout having to go through the traditional include() and require() directives. This helps PHP developers work without having to worry about whether the class that they need has already been loaded or not.

To achieve this, PHP 5 provides the __autoload() magic method that is called automatically whenever a class / object is being referenced while not being defined. If a definition exists for that class, the magic method will not be invoked. If a definition does not exist, the __autoload() method will be invoked before giving up and generating a warning / error message.

So how does auto loading work in examples?

The steps are pretty simple:

  1. Define a function __autoload($class) that will be automatically passed the name of the class that is being loaded.
  2. Provide an implementation for that function that tries to search / find / load the class as needed by the application.

Take the following example to be able to better explain this point.

Given fhe following directory structure within an application:

app |
.......| public
............| index.php
............| images
...................| ...
............| style
...................| ...
.......| library
............| Db
...................| Mysql.php
...................| Oracle.php
............| Output
...................| Xhtml.php
...................| Xml.php
...................| Ajax.php

if we try to load the mysql.php file  into index.php, our PHP code would normally look as follows:

include(ROOTDIR . "/app/library/Db/Mysql.php");

Assuming that ROOTDIR is a defined constant pointing to the root directory of our application, this statement will ask the PHP interpreter to load the file mysql.php from the given path.

The same will need to be done everytime we need to use any file from within the library folder. This, however, can be automagically done through the __autoload() magic function that was described above. The code below is what can be used and I will explain, after it, its many advantages.


function __autoload($className) {
    $filePath = str_replace("_", "/", $className);
    $fileName = LIBRARYDIR . '/' . $filePath . ".php";
    require_once $fileName;
}


The code above is taking the class name ($className) and replacing all occurrences with '_' with a '/' to build up the complete path to the file. If our class is named Db_Mysql, the__autoload() function will automatically build the path to the file and load it.

Inside the index.php file, we will simply use our class without having to include it first but we will need to pay attention to how we name our classes

An example of how the index.php file will look like will follow and is explained in details:

 


function __autoload($className) {
    $filePath = str_replace("_", "/", $className);
    $fileName = LIBRARYDIR . '/' . $filePath . ".php";
    require_once $fileName;

}

$x = new Db_Mysql();


When we try to create a new instance of Db_Mysql, the PHP interpreter will notice that it is not loaded and will automatically invoke the __autoload() function passing it Db_Mysql as the class name.

 

$filePath will hold the value Db/Mysql

$fileName will have the value  <path to application>/app/library/Db/Mysql.php

and the file will be automatically required.

It is worth noting that as of PHP 5.3, you can try to place the autoload() magic method within a try { } catch block. Any exception thrown while trying to load the file can be catched and, thus, different locations can be provided for autoloading.

Also, another nice feature is available with the Zend Autoloader which provides namespaces for loading objects. In other words, you can define many locations to be used for autoloading and the Zend Autoloader will search these locations every time a new object is being loaded.

Comments

8;L5 2 8?>B5:C

ethPseub | 21/03/2018, 14:27

8;L5 2 8?>B5:C http://arenda-s-vykupom.info
.

B5=45@=>5 A>?@>2>645=85 A0@0=A:

FrankBum | 21/03/2018, 12:47

B5=45@=>5 A>?@>2>645=85 A0@0=A:
http://ckpiz.info/page/tendernoe-soprovojdenie-saransk/
.

>2>AB8 :>3> <8@0 BCB

JesseDag | 15/02/2018, 14:25

>2>AB8 :>3> <8 bcb href="http://alvarvas.com/">alvarvas.com

!25685 :C;8=0@=K5 @5F5?BK BCB

Danielcig | 13/02/2018, 11:51

!25685 :C;8=0@=K5 @5F5?BK BCB cakeblog.su

>2>AB8 BC@87<0 BCB

Jameshat | 30/01/2018, 08:47

>2>AB8 BC@87<0 bcb href="http://zimnij-turizm.ru/">zimnij-turizm.ru

10 A?>A>1>2 >B:070BLAO >B E8<88 =0 40G5

StephenWhina | 28/01/2018, 18:51

!53>4=O 385 40G=8:8 >B:07K20NBAO >B 8A?>;L7>20=8O E82 8 8A?>;L7CNB ?@8=F8?K >@30=8G5A:>3> A5;LA:>3> E>7O9AB20, ?>72>;ONI53> ?>;CG0BL :0G5AB25==K9 C@>609 157 ?@81@5=89 8 ?5AB8F84>2. '8B09B5 >1 MB>4@>1=55 =0 A09B5 sovet-sadovody.ru

>2>AB8 :>3> <8@0 BCB

Elwoodalils | 25/01/2018, 08:59

>2>AB8 :>3> <8 bcb href="http://progio.ru/">progio.ru

'8AB:0 C;LB@072C:>2: :0: A45;0BL C;K1:C A25B;55 70 ?>;G0A0

Harolddem | 21/01/2018, 01:40

 B> 7C1K =C6=> G8AB8BL 42064K 2 45=L, <8 /> ?@028;0 B>;L:> 50% ;N459. >;55 B>3>, 8AA;54>20=8O H254A:8E CG5=KE ?>:070;8, GB> G8AB8B 7C1K ?> 2A530;L:> >48= G5;>25: 87 45AOB8, ?>4@>1=55 G8B09B5 =0 A09B5 nalatty.com

>2>AB8 <548F8=K BCB

MichaelRerve | 18/01/2018, 09:36

>2>AB8 <548F8 bcb href="http://griskomed.ru/">griskomed.ru

85B>;>38 =0720;8 7 A0 ;57=KE >2>I59

Davidgloda | 12/01/2018, 00:11

ABL =5A:>;L:> >2>I59, :>B>@K5 ?>;57=K 2=5 A57>=0 8 ?>B>1O70B5;L=> 4>;6=K 1KBL 2:;NG5=K 2 @0F8>= ?8B0=8O. 1 MB>4=8;57=59H8E >2>I59 >=8 =0720;8 >1K:=>25==CN :0?CABC. '8B09B5 >1 MB>4@>1=55 =0 A09B5 medinfon.ru

=>2>AB8 volkswagen jetta

aggarrRup | 10/01/2018, 19:33

 !& U W! U Q  U !!    U  W! T! Q T Q X  U! Q !S!! !9  U W! U!!9 W! U T! Q! U   Q! Q !!! U Q! !
!!  V U W! U  U - U!  U !! Q T!-1 Q T!-2
http://komn-dver.ru/
Q  U W Q! ! ! T  U!! U  U V U  U! Q !! ! Q  !! !9 X !!  ! U X !S  ! U !9 X U !! !!!
!!! U U   !& U X !!!  W Q! ! T !! Q !S ! V U Q U !  Q X  U !! U T !
hnnnnf567

nigar muharrem sensizlik

domp3.net | 07/01/2018, 08:20

http://domp3.net/images/do-mp3.png
jordan comolli 4u survivor eyes of a tiger descargar 27 4 beats joe moses ft yg gang bang dani corbalan resurrected original mix video edit zwirek

570=:>@=>5 @>42865=85

Traceeh | 18/11/2017, 23:31

@>42865=85 A09B>2.
>5 8=B5@=5B-?@>42865=85 A09B>2 157 ?>A@54=8:>2 8 ?5@5?;0B 2 >4=>42865=8O 2 8=B5@=5B5
-DD5:B82=> 8 =54>@>3>. 8A0BL 2 ICQ 656310373

Add comment
 
Accessible and Valid XHTML 1.0 Strict and CSS