[UPHPU] require_once vs. class_exists
Scott Mattocks
scott at crisscott.com
Fri Apr 18 10:23:21 MDT 2008
Rusty Keele wrote:
> Hi,
>
> Which is a better approach to use:
>
> 1. require_once('path/to/class.php');
>
> or
>
> 2. if(!class_exists('path/to/class.php')) require('path/to/class.php');
>
> I have been using the first approach in all my files where I need to instantiate a specific class, but I am wondering if the second approach is more efficient? Are there any benefits to using the second approach - such as less caching of objects - or do these two statements do the same thing?
It depends on which version of PHP you are using. require_once was not
very efficient in PHP 4 and early versions of PHP 5. I believe it has
been fixed in later versions of PHP 5.
On a side not, you are passing a file path to class_exists. That is
incorrect. You should pass the class name.
On another side note, require, include, require_once and include_once
are language constructs not functions. You don't need the () and some
would tell you that it is incorrect to add them.
Scott Mattocks
More information about the UPHPU
mailing list