note 50562 added to function.require-once
| From: | ulderico at maber dot com dot br | Date: | Thu, 03 Mar 2005 13:19:54 +0000 |
| Subject: | note 50562 added to function.require-once | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85866@lists.php.net to get a copy of this message | ||
For those who misses Java-like import , here's a handy implemention.
It may not well handle Singletons and References,
but I think it'll do fine for most cases...
Hey people do not steal it,
keep it GNU GPL please...
The following code is preseverd by GNU GPL:
function import($class){
$base_class = "PATHTO/WEB-INF/classes";
$class_strut = explode(".",$class);
if(sizeof($class_strut)<1)
trigger_error("too short class invocation");
if($class_strut[sizeof($class_strut)-1] == "*"){
$tmp_class_strut = $class_strut;
unset($tmp_class_strut[sizeof($tmp_class_strut)-1]);
$tmp_base_class = $base_class."/".
implode("/",$tmp_class_strut);
$d = dir($tmp_base_class);
while (false !== ($entry = $d->read())) {
if($entry == "." || $entry == "..")
continue;
require_once($tmp_base_class."/".$entry);
}
$d->close(); // Do you recognize this Example???
}else{
if(file_exists($base_class."/".
implode("/",$class_strut).".class.php"))
require_once($base_class."/".
implode("/",$class_strut).".class.php");
else
require_once($base_class."/".
implode("/",$class_strut).".php");
}
$vars = get_defined_vars();
foreach($vars as $var => $value){
$GLOBALS[$var] = $value;
}// Hey remember you're inside a function,
// you've got to globalise value here to outside...
}
----
Manual Page -- http://www.php.net/manual/en/function.require-once.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50562
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50562&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50562&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50562&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50562&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50562&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50562&report=yes
Search -- http://master.php.net/manage/user-notes.php