The Little Guy Posted July 3, 2012 Share Posted July 3, 2012 Am I doing this correctly? I am just learning on how to use namespaces in php, as I have never used them before. Cleep.php <?php namespace Cleep\Classes\Cleep; use Cleep\Classes\Template; class Cleep extends Template{ Template.php <?php namespace Cleep\Classes\Template; use Cleep\Classes\Base; class Template extends Base{ When I run it, I get the following error: Fatal error: Class 'Cleep\Classes\Template' not found in /home/ryannaddy/cms.cleep.us/classes/Cleep.php on line 4 I have a setup.php file which has __autoload() in it, and index.php includes it. Once I put the namesame and the use in, I got the above error. Quote Link to comment Share on other sites More sharing options...
xyph Posted July 3, 2012 Share Posted July 3, 2012 Namespaces don't save you from having to include the class before trying to extend or use it Also, you're kinda doing it wrong. It can be a complex subject though. I like how the PHP.net manual describes it like a filesystem, with folders. This page in particular might help you http://www.php.net/manual/en/language.namespaces.basics.php Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 3, 2012 Author Share Posted July 3, 2012 But I am including the file... <?php function __autoload($class){ if(is_file(__DIR__."/../classes/$class.php")){ require_once __DIR__."/../classes/$class.php"; } } $cleep = new Cleep(); Quote Link to comment Share on other sites More sharing options...
xyph Posted July 3, 2012 Share Posted July 3, 2012 But I am including the file... Sorry, missed the autoload. Your real issue is how you're name your spaces. The class name itself shouldn't be in the namespace calls. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 3, 2012 Author Share Posted July 3, 2012 Okay, so I removed the class name from the calls, so it now looks like this: namespace Cleep\Classes; use Cleep\Classes; class Cleep extends Template{ // And: namespace Cleep\Classes; use Cleep\Classes; class Template extends Base{ And I am still getting the same error. Quote Link to comment Share on other sites More sharing options...
xyph Posted July 3, 2012 Share Posted July 3, 2012 I edited it, you need them in your use keyword. Sorry. Here's an example: foobar.php <?php namespace foo; class bar { public function __construct() { echo 'foobar'; } } ?> run.php <?php include 'foobar.php'; $a = new \foo\bar; // needed, beacuse we are using the root namespace use \foo\bar; // reference \foo\bar into the root namespace $a = new bar; use \foo\bar as baz; // reference \foo\bar as baz into the root namespace $a = new baz; ?> The namespace keyword specifies the 'folder' that anything declared will reside in The use keyword references a particular 'file' to the current 'folder', saving you from having to type it's 'path' when you need to use it. Folder = namespace File = class/constant/function Path = namespace any given class/constant/function is in. Hope that helps. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 3, 2012 Author Share Posted July 3, 2012 Okay I am starting to feel really stupid, but I feel like I am doing exactly that, and yet I am still having the same problem. Here are my EXACT files (except for the template one that is too long too post). /includes/setup.php <?php require_once __DIR__."/settings.php"; function __autoload($class){ if(is_file(__DIR__."/../classes/$class.php")){ require_once __DIR__."/../classes/$class.php"; } } use Cleep\Classes\Cleep, Cleep\Classes\Template, Cleep\Classes\Hook; $cleep = new Cleep(); $library = $cleep->activeTemplate(); $plugins = $cleep->getActivePlugins(); $template = new Template($library); $hook = new Hook(); /index.php <?php /** * @var $temp Template * @var $plugins ArrayObject */ require_once "includes/setup.php"; $template->metaTags(array( "description" => "This is a test", "keywords" => "one,two,three" )); $hook->triggerHook("index.content"); $template->display("index.tpl"); /classes/Cleep.php <?php namespace Cleep\Classes; use Cleep\Classes\Template; class Cleep extends Template{ public function __construct(){ parent::__construct(); } public function activeTemplate(){ return $this->db->getOne("select directory from templates where is_active = 1"); } public function getActivePlugins(){ $plugins = array(); $this->db->query("select * from plugins where is_active = 1"); while($row = $this->db->row()){ $plugins[] = $row; } return $plugins; } } /classes/Template.php <?php namespace Cleep\Classes; use Cleep\Classes\Base; class Template extends Base{ //The rest of the file... Quote Link to comment Share on other sites More sharing options...
kicken Posted July 3, 2012 Share Posted July 3, 2012 The namespace is included in the parameter passed to your autoload function. So when loading the Template class for instance the variable $class in your autoload function will be 'Cleep\Classes\Cleep' not just 'Cleep' You need to strip off that namespace prefix before you test for the file's existance since you seem to just want the class name for your file test/require statements. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 3, 2012 Author Share Posted July 3, 2012 Why thank you kicken for the tip, and thanks xyph for the clarification! It now works! Quote Link to comment Share on other sites More sharing options...
Adam Posted July 3, 2012 Share Posted July 3, 2012 Also, you're kinda doing it wrong. It can be a complex subject though. I like how the PHP.net manual describes it like a filesystem, with folders. This page in particular might help you http://www.php.net/manual/en/language.namespaces.basics.php +1 -- there's no benefit in using namespaces the way you are doing. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 3, 2012 Author Share Posted July 3, 2012 Also, you're kinda doing it wrong. It can be a complex subject though. I like how the PHP.net manual describes it like a filesystem, with folders. This page in particular might help you http://www.php.net/manual/en/language.namespaces.basics.php +1 -- there's no benefit in using namespaces the way you are doing. Which part am I doing wrong? Quote Link to comment Share on other sites More sharing options...
xyph Posted July 3, 2012 Share Posted July 3, 2012 From what I understand it's purely for organization, and doesn't have benefits beyond that. You can accomplish pretty much everything namespacing offers without it, your code just may be a little 'uglier' http://www.sitepoint.com/php-53-namespaces-basics/ As the size of your PHP code library increases, there is increased risk of accidentally re-defining a function or class name that has been declared before. The problem is exacerbated when you attempt to add third-party components or plugins; what if two or more code sets implement a ?Database? or ?User? class? Until now, the only solution has been long class/function names. For example, WordPress prefixes every name with ?WP_?. The Zend Framework uses a highly descriptive naming convention that results in long-winded class names such as Zend_Search_Lucene_Analysis_Analyzer_Common_Text_CaseInsensitive. Name collision problems can be solved with namespaces. PHP constants, classes, and functions can be grouped into namespaced libraries. It's a nice way to organize vastly large applications. For a framework of your size, there's probably no point. The entire 'classes' sub-space is pretty redundant, as namespaces are pretty much just groups of similar classes. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 3, 2012 Author Share Posted July 3, 2012 Well mine is only small right now, I just started it 2 days ago The plan is for it to become a full blown plugin based CMS. Quote Link to comment Share on other sites More sharing options...
xyph Posted July 3, 2012 Share Posted July 3, 2012 My advice is to plan before you code Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 3, 2012 Author Share Posted July 3, 2012 My advice is to plan before you code So are you saying I am on the right track? Quote Link to comment Share on other sites More sharing options...
xyph Posted July 3, 2012 Share Posted July 3, 2012 We aren't in your head, so we don't know. The planning process can easily take as long as the programming side, and considering this is your first framework, I doubt it. It's all a learning process though. It took me till about my third core redesign before I scrapped the idea of designing while coding Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.