Jump to content
  • Advertisement
Sign in to follow this  
cignox1

PHP Class not found error

This topic is 2458 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!