Discussion:
Header files in PHP
Paul M Foster
2014-08-21 17:20:29 UTC
Permalink
Folks:

Over the years, I've heard a lot of back and forth about require_once,
require, include, include_once and he like. Most of it centers around
the CPU time taken to execute these calls.

The "C" way to do something similar is to define a constant in a header
file and then check for its existence in the C code file. In PHP, this
would be roughly like this:

LIBRARY FILE:

define('CONST_DATE', TRUE);

CODE FILE:

if (!defined('CONST_DATE'))
include('date.lib.php');

Has anyone ever compared execute times to see if something like this
would be "cheaper" than include/require[_once]? Any thoughts on it?

Paul
--
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
2014-08-21 20:19:44 UTC
Permalink
Post by Paul M Foster
Over the years, I've heard a lot of back and forth about require_once,
require, include, include_once and he like. Most of it centers around
the CPU time taken to execute these calls.
The "C" way to do something similar is to define a constant in a header
file and then check for its existence in the C code file. In PHP, this
define('CONST_DATE', TRUE);
if (!defined('CONST_DATE'))
include('date.lib.php');
Has anyone ever compared execute times to see if something like this
would be "cheaper" than include/require[_once]? Any thoughts on it?
Paul
I dont' understand your question. Basically the use of include/require
et al, is to include code from other stand-alone units. It's not just
looking for some one little thing such as a constant value to exist.
It's using a modular style of programming and putting the pieces
together at call time using require/include, etc.

Am I missing the question here?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Paul M Foster
2014-08-22 01:39:05 UTC
Permalink
Post by Jim Giner
Post by Paul M Foster
Over the years, I've heard a lot of back and forth about require_once,
require, include, include_once and he like. Most of it centers around
the CPU time taken to execute these calls.
The "C" way to do something similar is to define a constant in a header
file and then check for its existence in the C code file. In PHP, this
define('CONST_DATE', TRUE);
if (!defined('CONST_DATE'))
include('date.lib.php');
Has anyone ever compared execute times to see if something like this
would be "cheaper" than include/require[_once]? Any thoughts on it?
Paul
I dont' understand your question. Basically the use of
include/require et al, is to include code from other stand-alone
units. It's not just looking for some one little thing such as a
constant value to exist. It's using a modular style of programming
and putting the pieces together at call time using require/include,
etc.
Am I missing the question here?
Yes, you are. In C, code from file B is compiled separately from file A
(a "makefile" is used to coordinate the compilation of each and all
files). These are all standalone object files at the end of compilation.
In order for file A to actually *use* the code in file B, a header
file is written for the routines in file B, and that header file is
conditionally included in the compilation of file A. The above code is a
PHP approximation of how it's typically done in C.

In PHP, the mechanism used to include code from another file in the
current one involves one of four calls:

include()
include_once()
require()
require_once()

But over the years, I've read many comments about how each of these
calls slows down PHP, some more than others. So my question was whether
anyone had ever tested this mechanism against the way it's done in C (as
above) to determine if one is faster than the others.

(By the way, the C method of doing this is not dictated by the language,
but has arisen as a common method of accomplishing what the language
itself is not set up to do.)

Paul
--
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jeffry Killen
2014-08-22 02:31:02 UTC
Permalink
Post by Paul M Foster
Post by Jim Giner
Post by Paul M Foster
Over the years, I've heard a lot of back and forth about
require_once,
require, include, include_once and he like. Most of it centers around
the CPU time taken to execute these calls.
The "C" way to do something similar is to define a constant in a header
file and then check for its existence in the C code file. In PHP, this
define('CONST_DATE', TRUE);
if (!defined('CONST_DATE'))
include('date.lib.php');
Has anyone ever compared execute times to see if something like this
would be "cheaper" than include/require[_once]? Any thoughts on it?
Paul
I dont' understand your question. Basically the use of
include/require et al, is to include code from other stand-alone
units. It's not just looking for some one little thing such as a
constant value to exist. It's using a modular style of programming
and putting the pieces together at call time using require/include,
etc.
Am I missing the question here?
Yes, you are. In C, code from file B is compiled separately from file A
(a "makefile" is used to coordinate the compilation of each and all
files). These are all standalone object files at the end of
compilation.
In order for file A to actually *use* the code in file B, a header
file is written for the routines in file B, and that header file is
conditionally included in the compilation of file A. The above code is a
PHP approximation of how it's typically done in C.
In PHP, the mechanism used to include code from another file in the
include()
include_once()
require()
require_once()
But over the years, I've read many comments about how each of these
calls slows down PHP, some more than others. So my question was whether
anyone had ever tested this mechanism against the way it's done in C (as
above) to determine if one is faster than the others.
(By the way, the C method of doing this is not dictated by the
language,
but has arisen as a common method of accomplishing what the language
itself is not set up to do.)
Paul
As a commentary on this issue, it seems to me that one would have to
look
at the source code that is compiled into the interpreter and see exactly
what the various include/require statements/functions actually do. With
out looking at that myself I am guess that the include/require functions
just establish a hard link to the included script files.
Just a guess.
JK
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph Michael Becker
2014-08-22 11:28:07 UTC
Permalink
Post by Paul M Foster
In PHP, the mechanism used to include code from another file in the
include()
include_once()
require()
require_once()
But over the years, I've read many comments about how each of these
calls slows down PHP, some more than others. So my question was whether
anyone had ever tested this mechanism against the way it's done in C (as
above) to determine if one is faster than the others.
As a commentary on this issue, it seems to me that one would have to look
at the source code that is compiled into the interpreter and see exactly
what the various include/require statements/functions actually do. With
out looking at that myself I am guess that the include/require functions
just establish a hard link to the included script files.
Just a guess.
FWIW: the relevant source code is in Zend/zend_vm_execute.h line 2670ff[1].

[1] <http://lxr.php.net/xref/PHP_5_5/Zend/zend_vm_execute.h#2670>
--
Christoph M. Becker
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sebastian Krebs
2014-08-21 20:20:39 UTC
Permalink
Post by Paul M Foster
Over the years, I've heard a lot of back and forth about require_once,
require, include, include_once and he like. Most of it centers around
the CPU time taken to execute these calls.
The "C" way to do something similar is to define a constant in a header
file and then check for its existence in the C code file. In PHP, this
define('CONST_DATE', TRUE);
if (!defined('CONST_DATE'))
include('date.lib.php');
Has anyone ever compared execute times to see if something like this
would be "cheaper" than include/require[_once]? Any thoughts on it?
First I'd evaluate, if it's worth it, when you use an opcode cache and an
autoloader. I'd guess there is no measurable (and it's all about
measurement) impact.
Post by Paul M Foster
Paul
--
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
github.com/KingCrunch
Loading...