Jesdisciple Posted November 6, 2007 Share Posted November 6, 2007 SOLVED: Apparently, a local server refers to itself by the file:/// protocol; this is reflected in that it gives the file:/// address in error messages. So, in my case, $library needs to be file:///C:/wamp/www/.I'm trying to implement __autoload() so the path can be abbreviated (and relative to a constant, predefined location), after Java's import statement. But PHP refuses to recognize my paths (which work fine in an anchor tag) as valid. What do I need to change?http://localhost/Jesdisciple/test.php <?php $library = 'http://localhost/'; function import($directions){ global $imports, $library; if(is_null($imports)){ $imports = array(); } $matched = preg_match('/((?:\\w++\\.)++)(\\*|\\w++)/', $directions, $groups); if($matched != 0){//in case they make match() output all matches later $dir = $library . preg_replace('/\./', '/', $groups[1]); $file = $groups[2];echo "<a href=\"$dir$file.php\">" . Jesdisciple::toString(realpath($dir . $file . '.php')) . "</a>";//This link (with text "false") always works, sends me to the arbitrary file specified in test.php. if(isset($file)){ if($file === '*'){ if(is_dir($dir) && $package = opendir($dir)){ while (($file = readdir($package)) !== false) { if(filetype($dir . $file) == 'php'){ $imports[] = $dir . $file; } } closedir($package); } }elseif(file_exists($dir . $file . '.php')){ $imports[] = $dir . $file . '.php'; }else{//I always end up here.echo 'Told ya so.'; } } }else{ $imports[] = $library . $directions; } } function __autoload($class_name){//could be more robust, but I'm just getting import() to work for now global $imports; for($i0 = 0; $i0 < count($imports); $i0++){ require_once $imports[$i0]; } } class Jesdisciple{ public static function toString($var){ if(is_bool($var)){ return $var ? 'true' : 'false'; } if(is_string($var)){ return "'$var'"; } } }?> (dummy Map.php <?php class Map{ public function __construct(){} }?> Link to comment Share on other sites More sharing options...
justsomeguy Posted November 6, 2007 Share Posted November 6, 2007 Are you sure you want to include them over HTTP? Do the included files send their PHP code to the browser? Link to comment Share on other sites More sharing options...
Jesdisciple Posted November 6, 2007 Author Share Posted November 6, 2007 I'm not well-versed in HTTP so it didn't occur to me in that way, but see the top of my first post.But now I have another problem, and it seems like a bug in PHP.(whitespace printed as shown) <?php $library = 'file:///C:/wamp/www/'; function import($directions){ global $imports, $library; if(!is_array($imports)){ $imports = array(); } $matched = preg_match('/((?:\\w++\\.)++)(\\*|\\w++)/', $directions, $groups); if($matched != 0){//in case they make match() output all matches later $package = $groups[1]; $dir = $library . preg_replace('/\\./', '/', $package); $file = $groups[2]; if(isset($file)){ if($file == '*'){ if(is_dir($dir) && $package = opendir($dir)){ while (($file = readdir($package)) !== false){ if(pathinfo($dir . $file, PATHINFO_EXTENSION) == 'php'){//PROBLEM LINE - //(filetype($dir . $file) == 'php') consistently returns false but generates no error. $imports[] = $dir . $file; } } closedir($package); } }elseif(file_exists($dir . $file . '.php')){ $imports[] = $dir . $file . '.php'; } } }else{ $imports[] = $library . $directions; } }// <- Line 33 function __autoload($class_name){ global $imports; for($i0 = 0; $i0 < count($imports); $i0++){ require_once $imports[$i0]; } } class Jesdisciple{ public static function toString($var){ if(is_bool($var)){ return $var ? 'true' : 'false'; } if(is_string($var)){ return "'$var'"; } } }?> Link to comment Share on other sites More sharing options...
justsomeguy Posted November 7, 2007 Share Posted November 7, 2007 I'm confused. Unless I'm mistaken, this is the import function: function import($directions){ global $imports, $library; if(!is_array($imports)){ $imports = array(); } $matched = preg_match('/((?:\\w++\\.)++)(\\*|\\w++)/', $directions, $groups); if($matched != 0){//in case they make match() output all matches later $package = $groups[1]; $dir = $library . preg_replace('/\\./', '/', $package); $file = $groups[2]; if(isset($file)){ if($file == '*'){ if(is_dir($dir) && $package = opendir($dir)){ while (($file = readdir($package)) !== false){ if(pathinfo($dir . $file, PATHINFO_EXTENSION) == 'php'){//PROBLEM LINE - //(filetype($dir . $file) == 'php') consistently returns false but generates no error. $imports[] = $dir . $file; } } closedir($package); } }elseif(file_exists($dir . $file . '.php')){ $imports[] = $dir . $file . '.php'; } } }else{ $imports[] = $library . $directions; } }// <- Line 33 If you indicated that the last line is line 33, then by my calculations that makes the first line number 6. So the error message says that it can't declare import on line 33, because it was already declared in line 6... that doesn't make sense. Try changing the function name, see if import is reserved for future use.Also make sure you aren't including this file more then once. Use include_once and require_once whenever you do instead of include or require. Link to comment Share on other sites More sharing options...
Jesdisciple Posted November 7, 2007 Author Share Posted November 7, 2007 SOLVED: $library/generics/ included another version of test.php (Oops!), which was including (with require) Jesdisciple.php again. Thanks!Exactly! That's why I can only explain this error as a bug in Zend or some other component of PHP. And this line: function port($directions){ global $imports, $library; if(!is_array($imports)){ $imports = array(); } $matched = preg_match('/((?:\\w++\\.)++)?+(\\*|\\w++)/', $directions, $groups); if($matched != 0){//in case they make match() output all matches later $package = $groups[1]; $dir = $library . preg_replace('/\\./', '/', $package); $file = $groups[2]; if(isset($file)){ if($file == '*'){ if(is_dir($dir) && $package = opendir($dir)){ while (($file = readdir($package)) !== false){ if(pathinfo($dir . $file, PATHINFO_EXTENSION) == 'php'){ $imports[] = $dir . $file; } } closedir($package); } }elseif(file_exists($dir . $file . '.php')){ $imports[] = $dir . $file . '.php'; } } }else{ $imports[] = $library . $directions; } } Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.