PHP Classes and Objects Autoloading Class Files upon Object Instantiation - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Classes and Objects Autoloading Class Files upon Object Instantiation - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Monday, May 27, 2019

PHP Classes and Objects Autoloading Class Files upon Object Instantiation

PHP Classes and Objects




Autoloading Class Files upon Object Instantiation

Problem

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.


Solution

Use the __autoload() magic method:

         function __autoload($class_name) {
                include "$class_name.php";
         }

         $person = new Person;


Discussion

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:

         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:

         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.





No comments:

Post a Comment

Post Top Ad