Home arrow PHP arrow Auto Loading Classes in PHP 5

Auto Loading Classes in PHP 5

Undoubtedly, the release of PHP 5 has had a remarkable impact on the way that object-oriented applications are developed nowadays. This highly-improved model has provided PHP programmers with features that were only present in mature object-based languages, like Java and C++, but now, fortunately for you and me, they are generously offered by this powerful server-side scripting language.

TABLE OF CONTENTS:
  1. Auto Loading Classes in PHP 5
  2. Traditional Approach
  3. Traditional Approach continued
  4. Using the Magic Function
  5. Comparing the two
By: Alejandro Gervasio
Rating: starstarstarstarstar / 21
December 04, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Of course, if you’ve been using PHP 5 for a while to build your object-oriented applications, then it’s likely that you’re already familiar with creating classes, defining their corresponding methods and properties, working with exceptions and interfaces, and so forth.

However, even when all of the features that I highlighted actually comprise the foundations of the so-called object-based paradigm, it’d be rather unfair not to mention other useful characteristics that come packaged with PHP 5, which can help facilitate the development of object-oriented projects.

Destructors and object deferencing are good examples of certain characteristics available in PHP 5, which can be extremely helpful when working with objects that need to be called when they no longer exist in the context of a given web application, or when a certain number of these objects must be chained with each other through their corresponding APIs.

Nevertheless, among the improvements introduced into the object model provided by PHP 5, there’s one in particular that can be considered a programmer's best friend when using objects, due to its remarkable functionality. Do you think that I’m being too verbose here? Well, maybe, but this won’t stop me from telling you loud and clear the name of the improvement in question. Yes! You guessed right! In this case, I’m talking about the useful “__autoload()” PHP 5 magic function, which can be really magical when it comes to automatically loading all the class files required by a determined PHP application.

As you might know, the “__autoload()” function, when used in a clever way, can eliminate almost completely the need to use the “require()/require_once()” and “include()/include_once()” PHP native functions to load your nicely-crafted classes automatically during the execution of an object-oriented application. This certainly speaks for itself about the powerful capabilities offered through this function.

Personally, when I started “auto loading” all of my PHP 5 classes with this magic function, I realized that it was one of those things one can’t live without. Hopefully after you go through this series of articles, you’ll feel the same way.

Actually I’m getting ahead of myself, so now is the perfect time to move forward and start learning how to put the “__autoload()” magic function to work for you, and load your classes without having to include them manually into your PHP 5 object-oriented applications. It’s going to be an educational experience. Believe me!



 
 
>>> 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: