Jump to content

Namespaces "Class Not Found"


The Little Guy

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.