NomadicJosh Posted July 29, 2012 Share Posted July 29, 2012 I want to convert my MVC framework to conform to the PSR-0 standards, but I am having a devil of a time trying to wrap my head around namespaces and how it works. I am using the suggested SplClassLoader class. Below is my file structure, and what I am trying to accomplish. index.php application system --Classes --Autoloader.php --Extension.php --config --helper.php index.php: defined( 'DS' ) or define( 'DS', DIRECTORY_SEPARATOR ); defined( 'BASE_PATH' ) or define( 'BASE_PATH', dirname(realpath(__FILE__)) . DS ); defined( 'APP_FOLDER' ) or define( 'APP_FOLDER', 'application' ); defined( 'APP_PATH' ) or define( 'APP_PATH', BASE_PATH . APP_FOLDER . DS ); defined( 'SYS_PATH' ) or define( 'SYS_PATH', BASE_PATH . 'system' . DS ); defined( 'LOCALE_DIR' ) or define( 'LOCALE_DIR', BASE_PATH . 'locale' ); defined( 'DEFAULT_LOCALE' ) or define( 'DEFAULT_LOCALE', ''); defined( 'ENCODING' ) or define( 'ENCODING', 'UTF-8'); defined( 'CURRENT_VERSION' ) or define( 'CURRENT_VERSION', '0.3'); require( SYS_PATH . 'config/constants.php'); require( SYS_PATH . 'application.php'); require( SYS_PATH . 'Classes/Autoloader.php'); use Classes\Autoloader; $classLoader = new Autoloader('Classes', BASE_PATH . 'system'); $classLoader->register(); $app = new Bootstrap(); Extension.php: <?php if ( ! defined('BASE_PATH')) exit('No direct script access allowed'); namespace Classes; class Extension { public __construct() { echo 'This Extension class is working'; } } helper.php <?php if ( ! defined('BASE_PATH')) exit('No direct script access allowed'); use Classes\Extension as Ext; $apphelp = new Ext(); $apphelp->helper(array('gettext')); The helper.php file is automatically loaded through a registry.php file, so there are no issues there. Now that all that is place, I am getting the following error: Fatal error: Class 'Classes\Extension' not found in /Applications/MAMP/htdocs/framework/system/config/helper.php on line 21 I can't figure out what I am doing wrong. Like I said, I am still trying to understand namespaces, but I thought I was making progress, but I've been struggling with this for two days now. I know that "namespace" is supposed to come first in the code, and I did make that change even though it doesn't look so above, but I still receive errors. Any help is greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/266430-help-with-understanding-namespaces/ Share on other sites More sharing options...
ManiacDan Posted July 30, 2012 Share Posted July 30, 2012 Are you using include() or require() to pull in the file? I don't see it Link to comment https://forums.phpfreaks.com/topic/266430-help-with-understanding-namespaces/#findComment-1365406 Share on other sites More sharing options...
NomadicJosh Posted July 30, 2012 Author Share Posted July 30, 2012 It's in the autoloader class near the bottom: <?php /** * SplClassLoader implementation that implements the technical interoperability * standards for PHP 5.3 namespaces and class names. * * http://groups.google.com/group/php-standards/web/final-proposal * * // Example which loads classes for the Doctrine Common package in the * // Doctrine\Common namespace. * $classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine'); * $classLoader->register(); * * @author Jonathan H. Wage <[email protected]> * @author Roman S. Borschel <[email protected]> * @author Matthew Weier O'Phinney <[email protected]> * @author Kris Wallsmith <[email protected]> * @author Fabien Potencier <[email protected]> */ class SplClassLoader { private $_fileExtension = '.php'; private $_namespace; private $_includePath; private $_namespaceSeparator = '\\'; /** * Creates a new <tt>SplClassLoader</tt> that loads classes of the * specified namespace. * * @param string $ns The namespace to use. */ public function __construct($ns = null, $includePath = null) { $this->_namespace = $ns; $this->_includePath = $includePath; } /** * Sets the namespace separator used by classes in the namespace of this class loader. * * @param string $sep The separator to use. */ public function setNamespaceSeparator($sep) { $this->_namespaceSeparator = $sep; } /** * Gets the namespace seperator used by classes in the namespace of this class loader. * * @return void */ public function getNamespaceSeparator() { return $this->_namespaceSeparator; } /** * Sets the base include path for all class files in the namespace of this class loader. * * @param string $includePath */ public function setIncludePath($includePath) { $this->_includePath = $includePath; } /** * Gets the base include path for all class files in the namespace of this class loader. * * @return string $includePath */ public function getIncludePath() { return $this->_includePath; } /** * Sets the file extension of class files in the namespace of this class loader. * * @param string $fileExtension */ public function setFileExtension($fileExtension) { $this->_fileExtension = $fileExtension; } /** * Gets the file extension of class files in the namespace of this class loader. * * @return string $fileExtension */ public function getFileExtension() { return $this->_fileExtension; } /** * Installs this class loader on the SPL autoload stack. */ public function register() { spl_autoload_register(array($this, 'loadClass')); } /** * Uninstalls this class loader from the SPL autoloader stack. */ public function unregister() { spl_autoload_unregister(array($this, 'loadClass')); } /** * Loads the given class or interface. * * @param string $className The name of the class to load. * @return void */ public function loadClass($className) { if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) { $fileName = ''; $namespace = ''; if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) { $namespace = substr($className, 0, $lastNsPos); $className = substr($className, $lastNsPos + 1); $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; } $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension; require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName; } } } Link to comment https://forums.phpfreaks.com/topic/266430-help-with-understanding-namespaces/#findComment-1365412 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.