Guest User

PHP: get namespace from file

a guest
Apr 5th, 2011
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.51 KB | None | 0 0
  1. /*
  2.  * Is there a better way of determining a file's namespace than stripping it out using file functions?  Reflection doesn't seem to help.
  3.  * --Anatai
  4.  */
  5.  
  6.  
  7. /* file.php */
  8. <?php
  9.  
  10. namespace foo;
  11.  
  12. // rest of file here
  13.  
  14. ?>
  15.  
  16.  
  17. /* index.php */
  18. <?php
  19.  
  20. $h = fopen('file.php', 'r') or die('wtf');
  21. $n = '';
  22.  
  23. while (!feof($h) && !$n)
  24. {
  25.     $l = fgets($h);
  26.     if (stripos($l, 'namespace') !== false) $n = preg_replace('/namespace\ +(\w+);/i', '$1', $l);
  27. }
  28.  
  29. fclose($h);
  30. print $n ? trim($n) : 'Nothing found';
  31.  
  32. ?>
Advertisement
Add Comment
Please, Sign In to add comment