Re: include/require inside of function
| From: | Tom Rogers | Date: | Fri, 04 Jul 2003 08:12:23 +0000 |
| Subject: | Re: include/require inside of function | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-153972@lists.php.net to get a copy of this message | ||
Hi,
Friday, July 4, 2003, 5:11:18 PM, you wrote:
AC> Is there anyway to include a file inside of a function and have the included
AC> stuff be global? For instance if I defined a class or a function in that
AC> include file, I want to be able to use that class outside of the function.
AC> On the documentation for include() a poster commented that it did indeed
AC> work like this, but my testing indicates it does not. Everything stays
AC> local to the function and goes away when the function ends.
AC> Is there a way?
One trick that works in php-4.X.X is to put your function inside a
class, as any sub functions become global (invisible to the rest of the
class though :) but that should not matter). Not sure whether it will
be the same in php-5.
a quick example (ignore my filenames it was just to prove it works)
<?php
class loadClass {
function loadClass(){
//nothing to do yet;
}
function load($inc){
include($inc);
}
}
$l = new loadClass();
$l->load('templateClass.inc');
$t = new kwikTemplateClass('./');
$variables = array();
echo $t->processFile('layout.htm',$variables);
?>
This loads the class I need and it is automatically global..
--
regards,
Tom