Re: require/include a file inside a function [message #172449 is a reply to message #172447] |
Fri, 18 February 2011 15:00 |
Captain Paralytic
Messages: 204 Registered: September 2010
Karma:
|
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
|
|
|