note 103462 modified in function.require-once by danbrown
| From: | danbrown@php.net | Date: | Fri, 15 Apr 2011 14:00:23 +0000 |
| Subject: | note 103462 modified in function.require-once by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178875@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 == ""){ //no parameter returns the file import info tree;
$report = $_SESSION['imports'];
foreach($report as &$item) $item = array_flip($item);
return $report;
}
$current = str_replace("\\","/",getcwd())."/";
$path = $current.str_replace(".","/",$path);
if(substr($path,-1) != "*") $path .= ".class.php";
$imports = &$_SESSION['imports'];
if(!is_array($imports)) $imports = array();
$control = &$imports[$_SERVER['SCRIPT_FILENAME']];
if(!is_array($control)) $control = array();
foreach(glob($path) as $file){
$file = str_replace($current,"",$file);
if(is_dir($file)) import($file.".*");
if(substr($file,-10) != ".class.php") continue;
if($control[$file]) continue;
$control[$file] = count($control);
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
?>
--was--
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
?>
http://php.net/manual/en/function.require-once.php