Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding class required and exist

Tags:

php

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?

like image 483
Vladimir Kakazsky Avatar asked Jan 15 '12 20:01

Vladimir Kakazsky


People also ask

Can an object exist without a class?

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.

How do you define a class?

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.

What is the purpose of using 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.


4 Answers

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.

like image 139
Gilbert Kakaz Avatar answered Oct 21 '22 22:10

Gilbert Kakaz


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.

like image 45
TerryE Avatar answered Oct 21 '22 22:10

TerryE


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.

like image 20
esqew Avatar answered Oct 21 '22 22:10

esqew


  • require_once() only includes 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.

like image 43
Blender Avatar answered Oct 21 '22 20:10

Blender