[UPHPU] require_once vs. class_exists

Sean sean at lookin3d.com
Fri Apr 18 10:51:53 MDT 2008


This IMHO is the best way. Also the most optimal. The reason is you may 
or may not need to the class you are including at the top of your 
script. It could be contingent on a condition(s)

example

include 'Some_Class.php';

if(someCondition) {
    $class = new Some_Class();
} else {
   
}


__autoload make this more optimal by only including the classes you are 
actually using.



Lonnie Olson wrote:
> On Fri, 2008-04-18 at 09:13 -0700, 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?
>>     
>
> There is also a 3rd way... Autoloading.
> http://us2.php.net/autoload
> You can write your own custom __autoload function to require the files
> you need if a class doesn't exists.  It is very similar to #2, but can
> automate it for all classes.
>
> --lonnie
>
>
> _______________________________________________
>
> UPHPU mailing list
> UPHPU at uphpu.org
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net
>
>
>   


-- 

Sean Thayne,
Exit12



More information about the UPHPU mailing list