InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Autoloading Class Files upon Object Instantiation in PHP
PHP

Autoloading Class Files upon Object Instantiation in PHP

InfinityCoder November 25, 2016

You don’t want to include all your class definitions within every page. Instead, you want to dynamically load only the ones necessary in that page.

Use the __autoload() magic method:

1
2
3
4
5
function __autoload($class_name) {
    include "$class_name.php";
}
 
$person = new Person;

When you normally attempt to instantiate a class that’s not defined, PHP dies with a fatal error because it can’t locate what you’re looking for.

Therefore, it’s typical to load in all the potential classes for a page, regardless of whether they’re actually invoked.
This has the side effect of increasing processing time, because PHP must parse every class, even the unused ones. One solution is to load missing code on the fly using the __autoload() method, which is invoked when you instantiate undefined classes.
For example, here’s how you include all the classes used by your script:

1
2
3
4
5
function __autoload($class_name) {
    include "$class_name.php";
}
 
$person = new Person;

The __autoload() function receives the class name as its single parameter. This example appends a .php extension to that name and tries to include a file based on $class_name. So when you instantiate a new Person, it looks for Person.php in your include_path.
When __autoload() fails to successfully load a class definition for the object you’re trying to instantiate, PHP fails with a fatal error, just as it does when it can’t find a class definition without autoload.
If you adopt the PSR-0 naming convention, use the code at GitHub. Then you can do the following:

1
2
use Mysite\Person;
$person = new Person;

If the class isn’t defined, Person gets passed to __autoload(). The function pulls in the file based on the namespace and classname.
Though using __autoload() slightly increases processing time during the addition of a class, it is called only once per class.

Multiple instances of the same class does not result in multiple calls to __autoload(). Before deploying __autoload(), be sure to benchmark that the overhead of opening, reading, and closing the multiple files necessary isn’t actually more of a performance penalty than the additional parsing time of the unused classes.
In particular if you’re using an opcode cache, such as OPcache, using __autoload() and include_once can hurt performance.

For best results, you should include all your files at the top of the script and make sure you don’t reinclude a file twice.

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Using Non-Gregorian Calendars in PHP
You want to use a non-Gregorian calendar, such as a …

Using Non-Gregorian Calendars in PHP

Accessing Function Parameters in PHP
You want to access the values passed to a function. …

Accessing Function Parameters in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap