I have so far used "include" (coming from "C", I just saw it and
understood it).
Then I read about "require" and "require_once."
Ok. I understand that there might be some situations where not being
able to reach a particular file might not hinder the execution of an
applicaton (thus rationalizing the existence of "include"), but I hope
that will never be my case.
So I will rely on "require" over "include".
So far (I am absolutely new to PHP) I have written reusable functions
that are stored in files in the include folder, and have not needed to
nest inclusions.
But now I am going to start coding classes, and if they are going to
be completely context independent, they must declare their own
dependencies as "require" statements.
And I guess "require_once" is the natural choice for this situation.
Since I care a lot about simplicity, and lexical simpicity is achieved
by a vocabulary as reduced as possible, I am about to forget the
existence of "include", "include_once" and "require".
Is there any chance that such voluntary oblivion will get me into
trouble later (except for the chance to flunk in a certification test
that I will never take)?
Gregor Kofler Messages: 69 Registered: September 2010
Karma: 0
Member
Am 2011-01-21 17:51, Leonardo Azpurua meinte:
> Hi,
>
> I have so far used "include" (coming from "C", I just saw it and
> understood it).
>
> Then I read about "require" and "require_once."
>
> Ok. I understand that there might be some situations where not being
> able to reach a particular file might not hinder the execution of an
> applicaton (thus rationalizing the existence of "include"), but I hope
> that will never be my case.
>
> So I will rely on "require" over "include".
>
> So far (I am absolutely new to PHP) I have written reusable functions
> that are stored in files in the include folder, and have not needed to
> nest inclusions.
>
> But now I am going to start coding classes, and if they are going to be
> completely context independent, they must declare their own dependencies
> as "require" statements.
>
> And I guess "require_once" is the natural choice for this situation.
>
> Since I care a lot about simplicity, and lexical simpicity is achieved
> by a vocabulary as reduced as possible, I am about to forget the
> existence of "include", "include_once" and "require".
>
> Is there any chance that such voluntary oblivion will get me into
> trouble later (except for the chance to flunk in a certification test
> that I will never take)?
Perhaps not with thoughtful programming. I only use require_once.
But one could have code like this:
<?php
include 'initcalculation.php';
doCalculation1();
include 'initcalculation.php';
doCalculation2();
....
Looks obnoxious, but I've seen worse PHP scripts. After working through
those, I frequently feel like him [1].
sheldonlg Messages: 166 Registered: September 2010
Karma: 0
Senior Member
On 1/21/2011 11:51 AM, Leonardo Azpurua wrote:
> Hi,
>
> I have so far used "include" (coming from "C", I just saw it and
> understood it).
>
> Then I read about "require" and "require_once."
>
> Ok. I understand that there might be some situations where not being
> able to reach a particular file might not hinder the execution of an
> applicaton (thus rationalizing the existence of "include"), but I hope
> that will never be my case.
>
> So I will rely on "require" over "include".
>
> So far (I am absolutely new to PHP) I have written reusable functions
> that are stored in files in the include folder, and have not needed to
> nest inclusions.
>
> But now I am going to start coding classes, and if they are going to be
> completely context independent, they must declare their own dependencies
> as "require" statements.
>
> And I guess "require_once" is the natural choice for this situation.
>
> Since I care a lot about simplicity, and lexical simpicity is achieved
> by a vocabulary as reduced as possible, I am about to forget the
> existence of "include", "include_once" and "require".
>
> Is there any chance that such voluntary oblivion will get me into
> trouble later (except for the chance to flunk in a certification test
> that I will never take)?
>
> Thanks.
There is a use for require rather than require_once. Suppose, for
example, that you have a drop-down select list of the year (or anything
else). You might very well want to create an include file for all the
options in that list. If you have more than one date control (or any
other type of control that needs the same content), then it becomes a
simple and consistent matter to just define the dropdown and do a
"require" of the contents list file. In this case a "require_once"
would not be appropriate since it needs to be used more than once on the
page.
Otherwise, just use require_once all the time. It is safer. Also, it
will tell you in testing if you have made an error.
> [require vs. require_once]
>
> But now I am going to start coding classes, and if they are going to
> be completely context independent, they must declare their own
> dependencies as "require" statements.
>
> And I guess "require_once" is the natural choice for this situation.
In case of classes there's another very convenient option: autoloading.
Have a look at __autoload() or better spl_autoload_register().
"sheldonlg" <sheldonlg(at)thevillages(dot)net> wrote
>
> There is a use for require rather than require_once. Suppose, for
> example, that you have a drop-down select list of the year (or
> anything else). You might very well want to create an include file
> for all the options in that list. If you have more than one date
> control (or any other type of control that needs the same content),
> then it becomes a simple and consistent matter to just define the
> dropdown and do a "require" of the contents list file. In this case
> a "require_once" would not be appropriate since it needs to be used
> more than once on the page.
Yes. One might use include to insert a sequence of code in a
particular point of the output sequence.
Wouldn't in that case be better to write a function that outputs the
required HTML and then call that function where it is needed? Or else,
in the required file assign the desired string to a var and then
echoing the var? It seems cleaner to me.
Thank you very much. I hadn't thought about "include/require" in those
terms.
"Michael Fesser" <netizen(at)gmx(dot)de> escribió en el mensaje
news:1pfjj654tr4oeo45u6dkhu2lqqbto6r203(at)mfesser(dot)de...
> .oO(Leonardo Azpurua)
>
>> [require vs. require_once]
>>
>> But now I am going to start coding classes, and if they are going to
>> be completely context independent, they must declare their own
>> dependencies as "require" statements.
>>
>> And I guess "require_once" is the natural choice for this situation.
>
> In case of classes there's another very convenient option:
> autoloading.
>
> Have a look at __autoload() or better spl_autoload_register().
> I have so far used "include" (coming from "C", I just saw it and
> understood it).
>
> Then I read about "require" and "require_once."
For someone coming from C:
You'll have to understand that PHP include/require isn't a preprocessor
thing and PHP is an interpreted language, unlike C.
The difference between PHP include* vs. require* is that require will
cause a fatal error (stopping execution), include will only issue a
warning.
The *once behavior is a shortcut for the C sheme to make sure that a
file will only included once. Wherever the C equivalent would be
#ifdef INCLUDED_FOO
[whatever]
#define INCLUDED_FOO 1
#endif
you're choice in the PHP world would be *_once
As stated in this thread, there is a need to require/include Scripts in
PHP without the once thing because it's an interpreted language which
may produce different output when inluded in different status.
In practice, I use the require* and see rare use of the BC include
constructs.
"Leonardo Azpurua" wrote:
>> [require vs. require_once]
>>
>> But now I am going to start coding classes, and if they are going to
>> be completely context independent, they must declare their own
>> dependencies as "require" statements.
>>
>> And I guess "require_once" is the natural choice for this situation.
"Michael Fesser" wrote:
> In case of classes there's another very convenient option: autoloading.
>
> Have a look at __autoload() or better spl_autoload_register().
In such case, require() is sufficient because the autoloader will only
be called when the class has not yet been defined, and thus the file has
not yet been included / required. And require() has a (very tiny)
performance benefit over require_once() because the latter needs to
record and remember scripts that must not be reloaded. (OK, 2/3 of 3/5
of bugger all, but > 0)
--
Ross McKay, Toronto, NSW Australia
"There is more to life than simply increasing its speed." - Gandhi
> Leonardo Azpurua wrote:
>
>> I have so far used "include" (coming from "C", I just saw it
>> and understood it).
>>
>> Then I read about "require" and "require_once."
>
> For someone coming from C:
<snip>
> The *once behavior is a shortcut for the C sheme to make sure
> that a file will only included once. Wherever the C equivalent
> would be
>
> #ifdef INCLUDED_FOO
>
> [whatever]
> #define INCLUDED_FOO 1
> #endif
<OT>
As far as I can tell, this won't accomplish anything unless you
explicitly #define "INCLUDED_FOO" before the #include, which seems
an unnecessary hassle. On top of that, the "#define INCLUDED_FOO
1" would then be useless.
In C, implementing idempotent #include's generally looks more like:
#ifndef FOOBAR_H_
#define FOOBAR_H_
/* preprocessor stuff */
#endif
</OT>
> In practice, I use the require* and see rare use of the BC
> include constructs.
Interestingly, I came across Rasmus Lerdorf's* thoughts on the
matter:
| Try to avoid using include_once and require_once if
| possible. You are much better off using a straight
| include or require call, because the *_once() calls are
| very slow under an opcode cache. Sometimes there is no
| way around using these calls, but recognize that each
| one costs you an extra open() syscall and hash look up.