Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: require_once

0 views
Skip to first unread message

free...@centrum.cz

unread,
Sep 1, 2010, 4:56:54 PM9/1/10
to php-g...@lists.php.net
I think the files included just when you call it to include. How do you mean
it later?
What's in that file? A class or direct code?

David Mehler wrote:

> Hello,
> I've got probably a simple question on require_once. I've got a file
> that has require_once at the top of it pulling in another file of
> functions. Later on in this file I've got another require_once
> bringing in a second file. In this second file I have a function call
> to a function defined in the first files' top require_once functions
> file. I'm getting a call to undefined function.
> Should I change he require_once to require at the top of the first
> file to fix? I thought require_once meant it was in that file and
> anything pulled in later?
> Thanks.
> Dave.

Peter Lind

unread,
Sep 2, 2010, 6:59:07 AM9/2/10
to Jangita, php-g...@lists.php.net
On 2 September 2010 12:55, Jangita <jan...@jangita.com> wrote:
> If i get you correctly
>
> file1.php has a require_once to file2.php
> then file 1.php has a require_once to file3.php
> but file3.php calls a function in file2.php
>
> answer would be to require_onec file2.php in file3.php

Which wouldn't do anything seeing as file2.php has already been
included by file1.php.

Regards
Peter

--
<hype>
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
BeWelcome/Couchsurfing: Fake51
Twitter: http://twitter.com/kafe15
</hype>

Jangita

unread,
Sep 2, 2010, 7:20:22 AM9/2/10
to Peter Lind, php-g...@lists.php.net
On 02/09/2010 12:59 p, Peter Lind wrote:
> Which wouldn't do anything seeing as file2.php has already been
> included by file1.php.
Amazing! I didn't think that once you include a file; any other file
included also includes any earlier included files (if that makes any sence)

I've always thought includes are local; I.e in order to use a function
from another file explicitly include that other file.

--
Jangita | +256 76 91 8383 | Y! & MSN: jan...@yahoo.com
Skype: jangita | GTalk: jangita...@gmail.com

Jangita

unread,
Sep 2, 2010, 6:55:29 AM9/2/10
to php-g...@lists.php.net
On 01/09/2010 10:56 p, free...@centrum.cz wrote:

If i get you correctly

file1.php has a require_once to file2.php
then file 1.php has a require_once to file3.php
but file3.php calls a function in file2.php

answer would be to require_onec file2.php in file3.php

or?

David Mehler

unread,
Sep 1, 2010, 2:00:22 AM9/1/10
to php-general

Peter Lind

unread,
Sep 1, 2010, 2:39:22 AM9/1/10
to David Mehler, php-general

The file is included properly, otherwise your script would halt.
You're likely looking at an issue of scope: the functions might be
included in a scope you don't expect them to be.

Peter Lind

unread,
Sep 2, 2010, 7:55:17 AM9/2/10
to Jangita, php-g...@lists.php.net
On 2 September 2010 13:20, Jangita <jan...@jangita.com> wrote:
> On 02/09/2010 12:59 p, Peter Lind wrote:
>>
>> Which wouldn't do anything seeing as file2.php has already been
>> included by file1.php.
>
> Amazing! I didn't think that once you include a file; any other file
> included also includes any earlier included files (if that makes any sence)
>
> I've always thought includes are local; I.e in order to use a function from
> another file explicitly include that other file.

You're thinking about it from the wrong perspective. It's not a
question of a file including another file into it's own space, it's a
question of scope. If you're in the global scope, any file included
will get included into the global scope. If you're in a local scope
however, then other rules apply: variables in the file (that would
normally be in the global scope if you just executed the file) will be
in the local scope only, while classes and functions in the included
file will be imported into the global scope.

Also, require_once will require a file once only. Using it to require
the same file again from anywhere else will fail to include the file -
that's the whole point of _once.

0 new messages