note 103462 added to function.require-once
| From: | spark at limao dot com dot br | Date: | Thu, 14 Apr 2011 14:31:53 +0000 |
| Subject: | note 103462 added to function.require-once | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178829@lists.php.net to get a copy of this message | ||
if you use require_once on a file A pointing to file B, and require_once in the file B pointing to
file A, in some configurations you will get stuck
also wouldn't it be nice to manage that to prevent getting stuck AND use the good old Java
import?
<?php
function import($path=""){
if($path == "") return array_flip($_SESSION['imports']);
$current = str_replace("\\","/",getcwd())."/";
$path = $current.str_replace(".","/",$path).".class.php";
if(!is_array($_SESSION['imports'])) $_SESSION['imports'] = array();
foreach(glob($path) as $file){
$file = str_replace($current,"",$file);
if($_SESSION['imports'][$file]) continue;
$_SESSION['imports'][$file] = count($_SESSION['imports']);
require_once($file);
}
}
?>
just remember to start the session and to enable the glob function
now you can use
<?php
import("package.ClassName");
import("another.package.*"); //this will import everything in the folder
?>
----
Server IP: 69.147.83.197
Probable Submitter: 187.55.215.170
----
Manual Page -- http://www.php.net/manual/en/function.require-once.php
Edit -- https://master.php.net/note/edit/103462
Del: integrated -- https://master.php.net/note/delete/103462/integrated
Del: useless -- https://master.php.net/note/delete/103462/useless
Del: bad code -- https://master.php.net/note/delete/103462/bad+code
Del: spam -- https://master.php.net/note/delete/103462/spam
Del: non-english -- https://master.php.net/note/delete/103462/non-english
Del: in docs -- https://master.php.net/note/delete/103462/in+docs
Del: other reasons-- https://master.php.net/note/delete/103462
Reject -- https://master.php.net/note/reject/103462
Search -- https://master.php.net/manage/user-notes.php