FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Is there any situation where anything other than require_once is better?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Is there any situation where anything other than require_once is better? [message #171923] Fri, 21 January 2011 16:51 Go to next message
Leonardo Azpurua is currently offline  Leonardo Azpurua
Messages: 46
Registered: December 2010
Karma: 0
Member
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.

--
Re: Is there any situation where anything other than require_once is better? [message #171925 is a reply to message #171923] Fri, 21 January 2011 17:06 Go to previous messageGo to next message
Gregor Kofler is currently offline  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].

Gregor

[1]
&feature=related




--
vxweb.net
Re: Is there any situation where anything other than require_once is better? [message #171927 is a reply to message #171923] Fri, 21 January 2011 17:07 Go to previous messageGo to next message
sheldonlg is currently offline  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.

--
Shelly
Re: Is there any situation where anything other than require_once is better? [message #171928 is a reply to message #171923] Fri, 21 January 2011 17:19 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.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().

Micha
Re: Is there any situation where anything other than require_once is better? [message #171929 is a reply to message #171927] Fri, 21 January 2011 17:47 Go to previous messageGo to next message
Leonardo Azpurua is currently offline  Leonardo Azpurua
Messages: 46
Registered: December 2010
Karma: 0
Member
"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.

--
Leonardo
Re: Is there any situation where anything other than require_once is better? [message #171930 is a reply to message #171928] Fri, 21 January 2011 17:48 Go to previous messageGo to next message
Leonardo Azpurua is currently offline  Leonardo Azpurua
Messages: 46
Registered: December 2010
Karma: 0
Member
"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().

Great advice. Thanks!
Re: Is there any situation where anything other than require_once is better? [message #171933 is a reply to message #171923] Fri, 21 January 2011 20:59 Go to previous messageGo to next message
Jo Schulze is currently offline  Jo Schulze
Messages: 15
Registered: January 2011
Karma: 0
Junior Member
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:

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.

HTH
Re: Is there any situation where anything other than require_once is better? [message #171954 is a reply to message #171928] Sun, 23 January 2011 09:20 Go to previous messageGo to next message
Ross McKay is currently offline  Ross McKay
Messages: 14
Registered: January 2011
Karma: 0
Junior Member
"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
Re: Is there any situation where anything other than require_once is better? [message #171994 is a reply to message #171933] Wed, 26 January 2011 10:51 Go to previous message
Curtis Dyer is currently offline  Curtis Dyer
Messages: 34
Registered: January 2011
Karma: 0
Member
Jo Schulze <antispam(at)feuersee(dot)de> wrote:

> 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.

Source: <http://toys.lerdorf.com/archives/38-The-no-framework-PHP-
MVC-framework.html>

That post was last modified about a year ago, so I'm unsure if the
implementations of include's and require's were changed since then.


* Original implementer of PHP.

--
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Syntax for trim charlist?
Next Topic: SNMPv3 for PHP?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 27 20:13:16 GMT 2024

Total time taken to generate the page: 0.02756 seconds