I download a framework and code and I have a question regarding require
and include
vs class_exists
.
In the code I download, I see:
require_once('class.php');
As well as:
if(class_exists('class') == false) { require('class.php'); }
I get require_once
means only 1 time and class exists check if the class exists.
My question is: is the second better than the first one? and why?
No. Because at any point of time if you see, object have a state and not the class. In object all of its properties have values that you given or that are defined by default. Since static things are not related to state, you are adding them to Class and not to the object.
a class describes the contents of the objects that belong to it: it describes an aggregate of data fields (called instance variables), and defines the operations (called methods). object: an object is an element (or instance) of a class; objects have the behaviors of their class.
Classes are used to create and manage new objects and support inheritance—a key ingredient in object-oriented programming and a mechanism of reusing code.
this condition:
if(class_exists('class') == false) { require('class.php'); }
prevent the require_once
to be called.
require_once
can be slow if you have a lot of files you include in your project (specially frameworks) because they have to scan the code to make sure the file is not included twice or more. so if you have over 25 file you include and they are nested, require_once will have to check all of them.
As discussed in other answers there are are semantic differences between the two variants. Also in a shared hosting scenario where the hosting provider isn't offering PHP Opcode caching (APC or equiv), all code files need to be read in and compiled once per request. Here the main killer isn't the compile time itself (~0.5M PHP statements / sec on a modern core) but the I/O overhead of collecting the PHP files if not pre-cached in the file-system cache (10s of I/Os / sec).
In this scenario if an application requires a core of say a dozen modules for 90% of queries then it makes sense caching a bundle of these into a single bulkload file. If this type of solution is adopted then placing a class_exists('class')
guard around the require of the class file makes it bomb-proof.
Also a caution: If you use an __autoload()
function to do JiT class loading, be aware that class_exists('fred')
will trigger the autoload of fred
. If you want a weak probe then you need to use the predicate !in_array( 'fred', get_declared_classes() )
instead.
The second one would provide more protection from importing the class twice. The first one imports the class file once, but if the class has been imported from another file for some reason the second will pick up that the class has already been declared earlier and not require the classfile.
require_once()
only include
s a file once.
class_exists()
checks whether your class exists.
You can define a class in multiple files and require_once()
will not care. Your second chunk will.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With