Search the Community
Showing results for tags 'autoloading'.
-
Im trying to implement the PSR-0 autoloader found here : Autoloader I have simply copied and pasted the code as it is shown in the link so should work out of the box. Here is my file structure for my App: I believe i have set up the file structure correctly. App being the vendor, and then the folders below all being namespaces e.g. Frontend, Base, index will all be namespaces and subnamespaces. The autoloader classes specifies this code in order the initiate the autoloader: $classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine'); $classLoader->register(); And in my app i have used this: $classLoader = new SplClassLoader('App', SITE_ROOT); $classLoader->register(); The reason i have only put App as the namespace for the classLoader function is because i need to autoload all classes under the App folder. Here is the initiation of my router class which i believe should be autoloaded so i don't have to include it: $route = new router(); $route->setDefaultController(DEFAULT_CONTROLLER); $route->setDefaultMethod(DEFAULT_METHOD); $route->do_route(); $controller = $route->getController(); $method = $route->getMethod(); $arguments = $route->getArgs(); Is this correct? it doesnt seem to be loading any of the classes, not even the router class which is in the same directory as the autoloader? Im getting pretty confused about what should be parsed to the SPLClassLoader function shown above for a namespace. Any help in this area will be much appreciated Thanks
- 6 replies
-
- mvc
- autoloading
-
(and 2 more)
Tagged with:
-
I have recently started using OOP for my old procedural code and I'm getting the hang of it. The problem I have is with autoloading my class files. I have this code on each of my pages I'm redoing. require("php/classes/dbConstants.php"); function __autoload($className) { $extensions = array(".php", ".class.php", ".inc"); $paths = explode(PATH_SEPARATOR, get_include_path()); $className = str_replace("_" , DIRECTORY_SEPARATOR, $className); foreach ($paths as $path) { $filename = $path . DIRECTORY_SEPARATOR . $className; foreach ($extensions as $ext) { if (is_readable($filename . $ext)) { require_once $filename . $ext; break; } } } } try { $forum = new Database(DBHOST, DBUSER, DBPASS, DBNAME); } catch (Exception $e) { echo $e->getMessage(), "\n"; } but i'm getting this error message The file with the autoloader is in the root and the database class is in a folder called php and in a subfolder called classes. I need to adjust the path of where it's looking for my classes but no idea how Can anyone please help?