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

Home » Imported messages » comp.lang.php » require/include a file inside a function
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
require/include a file inside a function [message #172447] Fri, 18 February 2011 12:51 Go to next message
Simon is currently offline  Simon
Messages: 29
Registered: February 2011
Karma: 0
Junior Member
Hi,

is it possible to globally include(...) a file inside a function?

What I am trying to do is prevent loading files until they are really needed

currently I have something like...

// -----------------------
include 'large_a.php';
include 'large_b.php';
include 'large_c.php';
include 'large_d.php';

// -----------------------

but this is not ideal because I might never need those files...
I would much rather have something like

// -----------------------
function load_large_class( $a )
{
if( $a == $condition_a )
include 'large_a.php';
if( $a == $condition_b )
include 'large_b.php';
if( $a == $condition_c )
include 'large_c.php';
if( $a == $condition_d )
include 'large_d.php';
}
// -----------------------

but as you know that will only locally include the file, all classes,
variables etc will only 'exist' for the life of the function.

Would there be an easy way around this problem?

I guess I could always have a small file that says

// -----------------------
if( $a == $condition_a )
include 'large_a.php';
if( $a == $condition_b )
include 'large_b.php';
if( $a == $condition_c )
include 'large_c.php';
if( $a == $condition_d )
include 'large_d.php';
// -----------------------

but for the purpose of what I am trying to do I would prefer a function
as it would be neater to maintain/use.

Many thanks in advance

Simon
Re: require/include a file inside a function [message #172449 is a reply to message #172447] Fri, 18 February 2011 15:00 Go to previous messageGo to next message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
On Feb 18, 12:51 pm, Simon <b...@example.com> wrote:
> Hi,
>
> is it possible to globally include(...) a file inside a function?
>
> What I am trying to do is prevent loading files until they are really needed
>
> currently I have something like...
>
> // -----------------------
> include 'large_a.php';
> include 'large_b.php';
> include 'large_c.php';
> include 'large_d.php';
>
> // -----------------------
>
> but this is not ideal because I might never need those files...
> I would much rather have something like
>
> // -----------------------
> function load_large_class( $a )
> {
>    if( $a == $condition_a )
>     include 'large_a.php';
>    if( $a == $condition_b )
>     include 'large_b.php';
>    if( $a == $condition_c )
>     include 'large_c.php';
>    if( $a == $condition_d )
>     include 'large_d.php';}
>
> // -----------------------
>
> but as you know that will only locally include the file, all classes,
> variables etc will only 'exist' for the life of the function.
>
> Would there be an easy way around this problem?
>
> I guess I could always have a small file that says
>
> // -----------------------
> if( $a == $condition_a )
>   include 'large_a.php';
> if( $a == $condition_b )
>   include 'large_b.php';
> if( $a == $condition_c )
>   include 'large_c.php';
> if( $a == $condition_d )
>   include 'large_d.php';
> // -----------------------
>
> but for the purpose of what I am trying to do I would prefer a function
> as it would be neater to maintain/use.
>
> Many thanks in advance
>
> Simon

If you try it you will find that included code is included at the
point where the include is written. So if that it in a function, that
will be where the code is.

Take a look at the php magic function autoload:
http://www.phpro.org/tutorials/SPL-Autoload.html
Re: require/include a file inside a function [message #172484 is a reply to message #172449] Mon, 21 February 2011 05:39 Go to previous messageGo to next message
Simon is currently offline  Simon
Messages: 29
Registered: February 2011
Karma: 0
Junior Member
>
> If you try it you will find that included code is included at the
> point where the include is written. So if that it in a function, that
> will be where the code is.

Not sure I follow,

If I have something like

function fn_b()
{
include (a_big_file.php);
}

function fn_a()
{
fn_b();

// stuff, (functions, classes), created in fn_b() are not available
// to me here anymore.
// I am using 5.2.14 (dev) and 5.3.2 (live), (don't even ask me why!).
}

>
> Take a look at the php magic function autoload:
> http://www.phpro.org/tutorials/SPL-Autoload.html

Yes, this looks like it might actually help.

I will try, (I only got to work now), and see if files included in a
function that create a class still make the class globally available.

Simon
Re: require/include a file inside a function [message #172503 is a reply to message #172484] Mon, 21 February 2011 13:07 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/21/2011 12:39 AM, Simon wrote:
>>
>> If you try it you will find that included code is included at the
>> point where the include is written. So if that it in a function, that
>> will be where the code is.
>
> Not sure I follow,
>
> If I have something like
>
> function fn_b()
> {
> include (a_big_file.php);
> }
>
> function fn_a()
> {
> fn_b();
>
> // stuff, (functions, classes), created in fn_b() are not available
> // to me here anymore.
> // I am using 5.2.14 (dev) and 5.3.2 (live), (don't even ask me why!).
> }
>
>>
>> Take a look at the php magic function autoload:
>> http://www.phpro.org/tutorials/SPL-Autoload.html
>
> Yes, this looks like it might actually help.
>
> I will try, (I only got to work now), and see if files included in a
> function that create a class still make the class globally available.
>
> Simon
>

No, files included in a function are NEVER accessible globally. They
are only available in the function - it's part of what functions do.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: require/include a file inside a function [message #172518 is a reply to message #172503] Mon, 21 February 2011 15:02 Go to previous messageGo to next message
Simon is currently offline  Simon
Messages: 29
Registered: February 2011
Karma: 0
Junior Member
>
> No, files included in a function are NEVER accessible globally. They are
> only available in the function - it's part of what functions do.

Yes this is what I thought would happen, (as per my original post), but
the reply from Paul gave the impression that maybe I was wrong.

auoload is probably best for what I am trying to do, it will globally
include files needed, when they are needed.

I just need to come up with a 'nice' naming convention of some sort to
make sure I don't get myself in a mess over time.

Simon
Re: require/include a file inside a function [message #172538 is a reply to message #172518] Mon, 21 February 2011 17:34 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/21/2011 10:02 AM, Simon wrote:
>>
>> No, files included in a function are NEVER accessible globally. They are
>> only available in the function - it's part of what functions do.
>
> Yes this is what I thought would happen, (as per my original post), but
> the reply from Paul gave the impression that maybe I was wrong.
>
> auoload is probably best for what I am trying to do, it will globally
> include files needed, when they are needed.
>
> I just need to come up with a 'nice' naming convention of some sort to
> make sure I don't get myself in a mess over time.
>
> Simon
>

I just use appropriate naming conventions and require_once() the files
as I need them. That way I KNOW what I'm getting. That's not always
true with autoload.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: require/include a file inside a function [message #172660 is a reply to message #172447] Thu, 24 February 2011 02:01 Go to previous messageGo to next message
Twayne is currently offline  Twayne
Messages: 135
Registered: September 2010
Karma: 0
Senior Member
In news:8s78bbFd6qU1(at)mid(dot)individual(dot)net,
Simon <bad(at)example(dot)com> typed:
:: Hi,
::
:: is it possible to globally include(...) a file inside a
:: function?
::
:: What I am trying to do is prevent loading files until they
:: are really needed
::
:: currently I have something like...
::
:: // -----------------------
:: include 'large_a.php';
:: include 'large_b.php';
:: include 'large_c.php';
:: include 'large_d.php';
::
:: // -----------------------
::
:: but this is not ideal because I might never need those
:: files...
:: I would much rather have something like
::
:: // -----------------------
:: function load_large_class( $a )
:: {
:: if( $a == $condition_a )
:: include 'large_a.php';
:: if( $a == $condition_b )
:: include 'large_b.php';
:: if( $a == $condition_c )
:: include 'large_c.php';
:: if( $a == $condition_d )
:: include 'large_d.php';
:: }
:: // -----------------------
::
:: but as you know that will only locally include the file,
:: all classes, variables etc will only 'exist' for the life
:: of the function.
::
:: Would there be an easy way around this problem?
::
:: I guess I could always have a small file that says
::
:: // -----------------------
:: if( $a == $condition_a )
:: include 'large_a.php';
:: if( $a == $condition_b )
:: include 'large_b.php';
:: if( $a == $condition_c )
:: include 'large_c.php';
:: if( $a == $condition_d )
:: include 'large_d.php';
:: // -----------------------
::
:: but for the purpose of what I am trying to do I would
:: prefer a function
:: as it would be neater to maintain/use.
::
:: Many thanks in advance
::
:: Simon

Yes.
Re: require/include a file inside a function [message #172665 is a reply to message #172660] Thu, 24 February 2011 04:03 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/23/2011 9:01 PM, Twayne wrote:
> In news:8s78bbFd6qU1(at)mid(dot)individual(dot)net,
> Simon<bad(at)example(dot)com> typed:
> :: Hi,
> ::
> :: is it possible to globally include(...) a file inside a
> :: function?
> ::
> :: What I am trying to do is prevent loading files until they
> :: are really needed
> ::
> :: currently I have something like...
> ::
> :: // -----------------------
> :: include 'large_a.php';
> :: include 'large_b.php';
> :: include 'large_c.php';
> :: include 'large_d.php';
> ::
> :: // -----------------------
> ::
> :: but this is not ideal because I might never need those
> :: files...
> :: I would much rather have something like
> ::
> :: // -----------------------
> :: function load_large_class( $a )
> :: {
> :: if( $a == $condition_a )
> :: include 'large_a.php';
> :: if( $a == $condition_b )
> :: include 'large_b.php';
> :: if( $a == $condition_c )
> :: include 'large_c.php';
> :: if( $a == $condition_d )
> :: include 'large_d.php';
> :: }
> :: // -----------------------
> ::
> :: but as you know that will only locally include the file,
> :: all classes, variables etc will only 'exist' for the life
> :: of the function.
> ::
> :: Would there be an easy way around this problem?
> ::
> :: I guess I could always have a small file that says
> ::
> :: // -----------------------
> :: if( $a == $condition_a )
> :: include 'large_a.php';
> :: if( $a == $condition_b )
> :: include 'large_b.php';
> :: if( $a == $condition_c )
> :: include 'large_c.php';
> :: if( $a == $condition_d )
> :: include 'large_d.php';
> :: // -----------------------
> ::
> :: but for the purpose of what I am trying to do I would
> :: prefer a function
> :: as it would be neater to maintain/use.
> ::
> :: Many thanks in advance
> ::
> :: Simon
>
> Yes.
>
>

No.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: require/include a file inside a function [message #172667 is a reply to message #172665] Thu, 24 February 2011 05:20 Go to previous messageGo to next message
Simon is currently offline  Simon
Messages: 29
Registered: February 2011
Karma: 0
Junior Member
On 2/24/2011 6:03 AM, Jerry Stuckle wrote:
> On 2/23/2011 9:01 PM, Twayne wrote:
>> In news:8s78bbFd6qU1(at)mid(dot)individual(dot)net,
>> Simon<bad(at)example(dot)com> typed:
>> :: Hi,
>> ::
>> :: is it possible to globally include(...) a file inside a
>> :: function?
>> ::
>> :: What I am trying to do is prevent loading files until they
>> :: are really needed
>> ::
>> :: currently I have something like...
>> ::
>> :: // -----------------------
>> :: include 'large_a.php';
>> :: include 'large_b.php';
>> :: include 'large_c.php';
>> :: include 'large_d.php';
>> ::
>> :: // -----------------------
>> ::
>> :: but this is not ideal because I might never need those
>> :: files...
>> :: I would much rather have something like
>> ::
>> :: // -----------------------
>> :: function load_large_class( $a )
>> :: {
>> :: if( $a == $condition_a )
>> :: include 'large_a.php';
>> :: if( $a == $condition_b )
>> :: include 'large_b.php';
>> :: if( $a == $condition_c )
>> :: include 'large_c.php';
>> :: if( $a == $condition_d )
>> :: include 'large_d.php';
>> :: }
>> :: // -----------------------
>> ::
>> :: but as you know that will only locally include the file,
>> :: all classes, variables etc will only 'exist' for the life
>> :: of the function.
>> ::
>> :: Would there be an easy way around this problem?
>> ::
>> :: I guess I could always have a small file that says
>> ::
>> :: // -----------------------
>> :: if( $a == $condition_a )
>> :: include 'large_a.php';
>> :: if( $a == $condition_b )
>> :: include 'large_b.php';
>> :: if( $a == $condition_c )
>> :: include 'large_c.php';
>> :: if( $a == $condition_d )
>> :: include 'large_d.php';
>> :: // -----------------------
>> ::
>> :: but for the purpose of what I am trying to do I would
>> :: prefer a function
>> :: as it would be neater to maintain/use.
>> ::
>> :: Many thanks in advance
>> ::
>> :: Simon
>>
>> Yes.
>>
>>
>
> No.
>
>

Maybe.
Re: require/include a file inside a function [message #172679 is a reply to message #172665] Thu, 24 February 2011 16:54 Go to previous messageGo to next message
Twayne is currently offline  Twayne
Messages: 135
Registered: September 2010
Karma: 0
Senior Member
In news:ik4la0$a4l$3(at)news(dot)eternal-september(dot)org,
Jerry Stuckle <jstucklex(at)attglobal(dot)net> typed:
:: On 2/23/2011 9:01 PM, Twayne wrote:
::: In news:8s78bbFd6qU1(at)mid(dot)individual(dot)net,
::: Simon<bad(at)example(dot)com> typed:
::::: Hi,
:::::
::::: is it possible to globally include(...) a file inside a
::::: function?
:::::
::::: What I am trying to do is prevent loading files until
::::: they are really needed
:::::
::::: currently I have something like...
:::::
::::: // -----------------------
::::: include 'large_a.php';
::::: include 'large_b.php';
::::: include 'large_c.php';
::::: include 'large_d.php';
:::::
::::: // -----------------------
:::::
::::: but this is not ideal because I might never need those
::::: files...
::::: I would much rather have something like
:::::
::::: // -----------------------
::::: function load_large_class( $a )
::::: {
::::: if( $a == $condition_a )
::::: include 'large_a.php';
::::: if( $a == $condition_b )
::::: include 'large_b.php';
::::: if( $a == $condition_c )
::::: include 'large_c.php';
::::: if( $a == $condition_d )
::::: include 'large_d.php';
::::: }
::::: // -----------------------
:::::
::::: but as you know that will only locally include the file,
::::: all classes, variables etc will only 'exist' for the
::::: life of the function.
:::::
::::: Would there be an easy way around this problem?
:::::
::::: I guess I could always have a small file that says
:::::
::::: // -----------------------
::::: if( $a == $condition_a )
::::: include 'large_a.php';
::::: if( $a == $condition_b )
::::: include 'large_b.php';
::::: if( $a == $condition_c )
::::: include 'large_c.php';
::::: if( $a == $condition_d )
::::: include 'large_d.php';
::::: // -----------------------
:::::
::::: but for the purpose of what I am trying to do I would
::::: prefer a function
::::: as it would be neater to maintain/use.
:::::
::::: Many thanks in advance
:::::
::::: Simon
:::
::: Yes.
:::
:::
::
:: No.
::
::
:: --
:: ==================
:: Remove the "x" from my email address
:: Jerry Stuckle
:: JDS Computer Training Corp.
:: jstucklex(at)attglobal(dot)net
:: ==================

Stuck, maybe you can't, but I certainly can here and use such a mechanism on
two sites that are functioning perfectly. There will be gotchas in ANY
methodology but this one works well. Try it; you'll see.
I'll not make a troll fest out of this, so flail away if you wish; we've
all seen you at work by now.

HTH,

Twayne`
Re: require/include a file inside a function [message #172680 is a reply to message #172667] Thu, 24 February 2011 16:57 Go to previous messageGo to next message
Twayne is currently offline  Twayne
Messages: 135
Registered: September 2010
Karma: 0
Senior Member
In news:8sm84hFln8U1(at)mid(dot)individual(dot)net,
Simon <bad(at)example(dot)com> typed:
:: On 2/24/2011 6:03 AM, Jerry Stuckle wrote:
::: On 2/23/2011 9:01 PM, Twayne wrote:
:::: In news:8s78bbFd6qU1(at)mid(dot)individual(dot)net,
:::: Simon<bad(at)example(dot)com> typed:
:::::: Hi,
::::::
:::::: is it possible to globally include(...) a file inside a
:::::: function?
::::::
:::::: What I am trying to do is prevent loading files until
:::::: they are really needed
::::::
:::::: currently I have something like...
::::::
:::::: // -----------------------
:::::: include 'large_a.php';
:::::: include 'large_b.php';
:::::: include 'large_c.php';
:::::: include 'large_d.php';
::::::
:::::: // -----------------------
::::::
:::::: but this is not ideal because I might never need those
:::::: files...
:::::: I would much rather have something like
::::::
:::::: // -----------------------
:::::: function load_large_class( $a )
:::::: {
:::::: if( $a == $condition_a )
:::::: include 'large_a.php';
:::::: if( $a == $condition_b )
:::::: include 'large_b.php';
:::::: if( $a == $condition_c )
:::::: include 'large_c.php';
:::::: if( $a == $condition_d )
:::::: include 'large_d.php';
:::::: }
:::::: // -----------------------
::::::
:::::: but as you know that will only locally include the
:::::: file, all classes, variables etc will only 'exist' for
:::::: the life of the function.
::::::
:::::: Would there be an easy way around this problem?
::::::
:::::: I guess I could always have a small file that says
::::::
:::::: // -----------------------
:::::: if( $a == $condition_a )
:::::: include 'large_a.php';
:::::: if( $a == $condition_b )
:::::: include 'large_b.php';
:::::: if( $a == $condition_c )
:::::: include 'large_c.php';
:::::: if( $a == $condition_d )
:::::: include 'large_d.php';
:::::: // -----------------------
::::::
:::::: but for the purpose of what I am trying to do I would
:::::: prefer a function
:::::: as it would be neater to maintain/use.
::::::
:::::: Many thanks in advance
::::::
:::::: Simon
::::
:::: Yes.
::::
::::
:::
::: No.
:::
:::
::
:: Maybe.

lol, one does get into that realm, don't they? As I said in a previous post
there are always some gotchas in the background but in this case it works
quite well with PHP 5.2.5 I think it is.

HTH,

Twayne`
Re: require/include a file inside a function [message #172681 is a reply to message #172679] Thu, 24 February 2011 17:07 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article <ik62hj$uep$1(at)news(dot)eternal-september(dot)org>,
"Twayne" <nobody(at)devnull(dot)spamcop(dot)net> wrote:

> In news:ik4la0$a4l$3(at)news(dot)eternal-september(dot)org,
> Jerry Stuckle <jstucklex(at)attglobal(dot)net> typed:
> :: On 2/23/2011 9:01 PM, Twayne wrote:
> ::: In news:8s78bbFd6qU1(at)mid(dot)individual(dot)net,
> ::: Simon<bad(at)example(dot)com> typed:

> ::::: Many thanks in advance
> :::::
> ::::: Simon
> :::
> ::: Yes.
> ::
> :: No.
> ::
> :: --
> :: ==================
> :: Remove the "x" from my email address
> :: Jerry Stuckle
> :: JDS Computer Training Corp.
> :: jstucklex(at)attglobal(dot)net
> :: ==================
>
> Stuck, maybe you can't, but I certainly can here and use such a mechanism on
> two sites that are functioning perfectly. There will be gotchas in ANY
> methodology but this one works well. Try it; you'll see.
> I'll not make a troll fest out of this, so flail away if you wish; we've
> all seen you at work by now.

You clowns gonna learn to *snip*, one of these centuries?

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
Re: require/include a file inside a function [message #172682 is a reply to message #172681] Thu, 24 February 2011 17:24 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
On 2/24/2011 12:07 PM, Tim Streater wrote:
> You clowns gonna learn to *snip*, one of these centuries?

Why would they want to snip 100 years/ (Ok, ok, I saw the comma but
couldn't resist). I agree, though. One of these centuries those guys
have to learn to snip.

--
Shelly
Re: require/include a file inside a function [message #172685 is a reply to message #172679] Thu, 24 February 2011 19:02 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/24/2011 11:54 AM, Twayne wrote:
> In news:ik4la0$a4l$3(at)news(dot)eternal-september(dot)org,
> Jerry Stuckle<jstucklex(at)attglobal(dot)net> typed:
> :: On 2/23/2011 9:01 PM, Twayne wrote:
> ::: In news:8s78bbFd6qU1(at)mid(dot)individual(dot)net,
> ::: Simon<bad(at)example(dot)com> typed:
> ::::: Hi,
> :::::
> ::::: is it possible to globally include(...) a file inside a
> ::::: function?
> :::::
> ::::: What I am trying to do is prevent loading files until
> ::::: they are really needed
> :::::
> ::::: currently I have something like...
> :::::
> ::::: // -----------------------
> ::::: include 'large_a.php';
> ::::: include 'large_b.php';
> ::::: include 'large_c.php';
> ::::: include 'large_d.php';
> :::::
> ::::: // -----------------------
> :::::
> ::::: but this is not ideal because I might never need those
> ::::: files...
> ::::: I would much rather have something like
> :::::
> ::::: // -----------------------
> ::::: function load_large_class( $a )
> ::::: {
> ::::: if( $a == $condition_a )
> ::::: include 'large_a.php';
> ::::: if( $a == $condition_b )
> ::::: include 'large_b.php';
> ::::: if( $a == $condition_c )
> ::::: include 'large_c.php';
> ::::: if( $a == $condition_d )
> ::::: include 'large_d.php';
> ::::: }
> ::::: // -----------------------
> :::::
> ::::: but as you know that will only locally include the file,
> ::::: all classes, variables etc will only 'exist' for the
> ::::: life of the function.
> :::::
> ::::: Would there be an easy way around this problem?
> :::::
> ::::: I guess I could always have a small file that says
> :::::
> ::::: // -----------------------
> ::::: if( $a == $condition_a )
> ::::: include 'large_a.php';
> ::::: if( $a == $condition_b )
> ::::: include 'large_b.php';
> ::::: if( $a == $condition_c )
> ::::: include 'large_c.php';
> ::::: if( $a == $condition_d )
> ::::: include 'large_d.php';
> ::::: // -----------------------
> :::::
> ::::: but for the purpose of what I am trying to do I would
> ::::: prefer a function
> ::::: as it would be neater to maintain/use.
> :::::
> ::::: Many thanks in advance
> :::::
> ::::: Simon
> :::
> ::: Yes.
> :::
> :::
> ::
> :: No.
> ::
> ::
> Stuck, maybe you can't, but I certainly can here and use such a mechanism on
> two sites that are functioning perfectly. There will be gotchas in ANY
> methodology but this one works well. Try it; you'll see.
> I'll not make a troll fest out of this, so flail away if you wish; we've
> all seen you at work by now.
>
> HTH,
>
> Twayne`
>
>

OK, troll, you're resorting to personal attacks again - but that's
exactly like you.

The fact is - anything included from within a function is local to that
function only.



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: require/include a file inside a function [message #172686 is a reply to message #172685] Thu, 24 February 2011 20:02 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
On 2/24/2011 2:02 PM, Jerry Stuckle wrote:
> On 2/24/2011 11:54 AM, Twayne wrote:
>> In news:ik4la0$a4l$3(at)news(dot)eternal-september(dot)org,
>> Jerry Stuckle<jstucklex(at)attglobal(dot)net> typed:
>> :: On 2/23/2011 9:01 PM, Twayne wrote:
>> ::: In news:8s78bbFd6qU1(at)mid(dot)individual(dot)net,
>> ::: Simon<bad(at)example(dot)com> typed:
>> ::::: Hi,
>> :::::
>> ::::: is it possible to globally include(...) a file inside a
>> ::::: function?
>> :::::
>> ::::: What I am trying to do is prevent loading files until
>> ::::: they are really needed
>> :::::
>> ::::: currently I have something like...
>> :::::
>> ::::: // -----------------------
>> ::::: include 'large_a.php';
>> ::::: include 'large_b.php';
>> ::::: include 'large_c.php';
>> ::::: include 'large_d.php';
>> :::::
>> ::::: // -----------------------
>> :::::
>> ::::: but this is not ideal because I might never need those
>> ::::: files...
>> ::::: I would much rather have something like
>> :::::
>> ::::: // -----------------------
>> ::::: function load_large_class( $a )
>> ::::: {
>> ::::: if( $a == $condition_a )
>> ::::: include 'large_a.php';
>> ::::: if( $a == $condition_b )
>> ::::: include 'large_b.php';
>> ::::: if( $a == $condition_c )
>> ::::: include 'large_c.php';
>> ::::: if( $a == $condition_d )
>> ::::: include 'large_d.php';
>> ::::: }
>> ::::: // -----------------------
>> :::::
>> ::::: but as you know that will only locally include the file,
>> ::::: all classes, variables etc will only 'exist' for the
>> ::::: life of the function.
>> :::::
>> ::::: Would there be an easy way around this problem?
>> :::::
>> ::::: I guess I could always have a small file that says
>> :::::
>> ::::: // -----------------------
>> ::::: if( $a == $condition_a )
>> ::::: include 'large_a.php';
>> ::::: if( $a == $condition_b )
>> ::::: include 'large_b.php';
>> ::::: if( $a == $condition_c )
>> ::::: include 'large_c.php';
>> ::::: if( $a == $condition_d )
>> ::::: include 'large_d.php';
>> ::::: // -----------------------
>> :::::
>> ::::: but for the purpose of what I am trying to do I would
>> ::::: prefer a function
>> ::::: as it would be neater to maintain/use.
>> :::::
>> ::::: Many thanks in advance
>> :::::
>> ::::: Simon
>> :::
>> ::: Yes.
>> :::
>> :::
>> ::
>> :: No.
>> ::
>> ::
>> Stuck, maybe you can't, but I certainly can here and use such a
>> mechanism on
>> two sites that are functioning perfectly. There will be gotchas in ANY
>> methodology but this one works well. Try it; you'll see.
>> I'll not make a troll fest out of this, so flail away if you wish; we've
>> all seen you at work by now.
>>
>> HTH,
>>
>> Twayne`
>>
>>
>
> OK, troll, you're resorting to personal attacks again - but that's
> exactly like you.
>
> The fact is - anything included from within a function is local to that
> function only.

Wwwwweeeeellllllllllll, that's not strictly true. If you have globals
turned on, and define something as global, then won't it be visible
outside the function? If you define a session variable (super-global)
inside a function, doesn't it retain its value outside the function?

OK, I know that is not what you meant, but your statement is not exactly
100% accurate as written when read by and independent reader.

--
Shelly
Re: require/include a file inside a function [message #172687 is a reply to message #172681] Thu, 24 February 2011 20:39 Go to previous messageGo to next message
Twayne is currently offline  Twayne
Messages: 135
Registered: September 2010
Karma: 0
Senior Member
In news:timstreater-63D4A7(dot)17074224022011(at)news(dot)individual(dot)net,
Tim Streater <timstreater(at)waitrose(dot)com> typed:
:: In article <ik62hj$uep$1(at)news(dot)eternal-september(dot)org>,
:: "Twayne" <nobody(at)devnull(dot)spamcop(dot)net> wrote:

....

::
:: You clowns gonna learn to *snip*, one of these centuries?

My 2 ¢:


Actually, I'm rather happy to see the above comment, so few people trim
anything here, and a couple others who even trim what they're responding to.
Very few in this particular set of groups seem to understand the snipping
process and how to do it. One in particular provides great responses using
the Reply feature, but then snips everything but his own response.
For those who don't know: The generally most effective snipping is to
trim out what you are not resonding to, keep the part you are responding to,
and any supporting data such as OS, apps, verstions, etc., and done so with
inline posting for clarity if it consists of more than a paragraph or two.
Re: require/include a file inside a function [message #172688 is a reply to message #172687] Thu, 24 February 2011 21:05 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(Twayne)

> For those who don't know: The generally most effective snipping is to
> trim out what you are not resonding to, keep the part you are responding to,
> and any supporting data such as OS, apps, verstions, etc., and done so with
> inline posting for clarity if it consists of more than a paragraph or two.

While we're at it: The generally most effective way to mark a quote is
to prepend its lines with '>', not '::'. The first is a de-facto
standard in Usenet, the latter is just broken. So please fix that.

TIA
Micha
Re: require/include a file inside a function [message #172690 is a reply to message #172687] Fri, 25 February 2011 12:56 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Twayne wrote:
> In news:timstreater-63D4A7(dot)17074224022011(at)news(dot)individual(dot)net,
> Tim Streater <timstreater(at)waitrose(dot)com> typed:
> :: In article <ik62hj$uep$1(at)news(dot)eternal-september(dot)org>,
> :: "Twayne" <nobody(at)devnull(dot)spamcop(dot)net> wrote:
>
> ...
>
> ::
> :: You clowns gonna learn to *snip*, one of these centuries?
>
> My 2 ¢:
>
>
> Actually, I'm rather happy to see the above comment, so few people trim
> anything here, and a couple others who even trim what they're responding to.
> Very few in this particular set of groups seem to understand the snipping
> process and how to do it. One in particular provides great responses using
> the Reply feature, but then snips everything but his own response.
> For those who don't know: The generally most effective snipping is to
> trim out what you are not resonding to, keep the part you are responding to,
> and any supporting data such as OS, apps, verstions, etc., and done so with
> inline posting for clarity if it consists of more than a paragraph or two.
>
>
>

...and then you get accused of responding out of context!

It is actually not as simple as it appears.
Re: require/include a file inside a function [message #172705 is a reply to message #172690] Sat, 26 February 2011 18:35 Go to previous message
Twayne is currently offline  Twayne
Messages: 135
Registered: September 2010
Karma: 0
Senior Member
In news:ik88u3$3kj$2(at)news(dot)albasani(dot)net,
The Natural Philosopher <tnp(at)invalid(dot)invalid> typed:
:: Twayne wrote:
::: In
::: news:timstreater-63D4A7(dot)17074224022011(at)news(dot)individual(dot)net,
::: Tim Streater <timstreater(at)waitrose(dot)com> typed:
::::: In article <ik62hj$uep$1(at)news(dot)eternal-september(dot)org>,
::::: "Twayne" <nobody(at)devnull(dot)spamcop(dot)net> wrote:
:::
::: ...
:::
:::::
::::: You clowns gonna learn to *snip*, one of these
::::: centuries?
:::
::: My 2 B":
:::
:::
::: Actually, I'm rather happy to see the above comment, so
::: few people trim anything here, and a couple others who
::: even trim what they're responding to. Very few in this
::: particular set of groups seem to understand the snipping
::: process and how to do it. One in particular provides
::: great responses using the Reply feature, but then
::: snips everything but his own response. For those who
::: don't know: The generally most effective snipping is to
::: trim out what you are not resonding to, keep the part you
::: are responding to, and any supporting data such as OS,
::: apps, verstions, etc., and done so with inline posting
::: for clarity if it consists of more than a paragraph or
::: two.
:::
:::
:::
::
:: ..and then you get accused of responding out of context!
::
:: It is actually not as simple as it appears.

IMO the first rule is "When in Rome... ."
Finding no consensus in the existing posts, then the backup is the RFC
netiquette suggestions, in order of preference, Inline, Bottom Posted, and
Top Posted least preferred, and TRIMMED for relevance in any case, being
sure to leave spec-type info such as system descrips, RAM, et al that may be
relevant.

HTH,

Twayne`
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Proxy to open blocked sites
Next Topic: problem with web service
Goto Forum:
  

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

Current Time: Fri Nov 22 21:34:24 GMT 2024

Total time taken to generate the page: 0.02701 seconds