PHP Class not found error

Started by
2 comments, last by cignox1 12 years, 1 month ago
Hi all, in order to improve my PHP knowledge, I'm trying to build a small framework with the goal to use it to develop my music band site.

I layed out a simple configuration class (just a placeholder actually):[php]<?php
/*
Class to handle all application wide Quarkweb configuration parameters.
*/
namespace quarkweb;

class QWConfiguration
{
protected static $mClassName = "QWConfiguration"; //By changing this before the configuration is read, an application can force the use of a specified configuration class
public function __construct()
{
//initialize the values
}

public static function getClassName()
{
return QWConfiguration::$mClassName;
}
public static function setClassName($aClassName)
{
QWConfiguration::$mClassName = $aClassName;
}

static public function getConfiguration()
{
$aClassName = QWConfiguration::getClassName();
$aClass = new $aClassName();
return $aClass;
}
}
?>[/php]

The idea is that it should work as a simple factory: the application sets the desidered class name for its own configuration and at the proper time an instance will be created. I've used the dynamic class instancing elsewhere without issues.
But I get the following error:

Fatal error: Class 'QWConfiguration' not found in C:\inetpub\wwwroot\quarkweb\core\qwconfiguration.php on line 28

I bet this is one of those errors that any half-experienced PHP programmer will spot in two seconds. Any idea?

Thank you!
Advertisement
It has to do with the fact that you're using a namespace (removing the namespace would cause your code to immediately work). I don't know much about PHP namespaces (I spent the past two years doing PHP development and never once used them), but you should try constructing the string that points to the namespace. Instead of

[php]$aClassName = QWConfiguration::getClassName();[/php]

you should try:

[php]$aClassName = "\\quarkweb\\" . QWConfiguration::getClassName();[/php]

Let me know if that works.

It has to do with the fact that you're using a namespace (removing the namespace would cause your code to immediately work). I don't know much about PHP namespaces (I spent the past two years doing PHP development and never once used them), but you should try constructing the string that points to the namespace. Instead of

[php]$aClassName = QWConfiguration::getClassName();[/php]

you should try:

[php]$aClassName = "\\quarkweb\\" . QWConfiguration::getClassName();[/php]

Let me know if that works.

Haven't really heard of php namespacing myself until not too long ago. Apparently it's biggest benifactor is it saves headaches on large projects with clashing names. Any other benifits to namespacing?
Yes, the problem was the namespace. I made it work just a couple of days ago but thank you nonetheless.


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

Haven't really heard of php namespacing myself until not too long ago. Apparently it's biggest benifactor is it saves headaches on large projects with clashing names. Any other benifits to namespacing?

[/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[/quote]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

I come from c++/CSharp and Java, so it was natural for me to use namespaces as soon as I learned that they existed in PHP. I suppose that the benefits are the same as they are in other languages: avoiding name clashing and better logical separation among different component of the application.

[/font]

This topic is closed to new replies.

Advertisement