0

I'm working on a Symfony2 project. For useful technical pratictes, I need to import external libraries. So I did it. But this library creates somes *_Exception class who extend from Exception.

My external library file ends with:

class CloudKey_Exception extends Exception {}
class CloudKey_RPCException extends CloudKey_Exception {public $data = null;}
class CloudKey_ProcessorException extends CloudKey_RPCException {}
class CloudKey_TransportException extends CloudKey_RPCException {}
class CloudKey_SerializerException extends CloudKey_RPCException {}
class CloudKey_AuthenticationErrorException extends CloudKey_RPCException {}
class CloudKey_RateLimitExceededException extends CloudKey_AuthenticationErrorException {}

class CloudKey_InvalidRequestException extends CloudKey_RPCException {}
class CloudKey_InvalidObjectException extends CloudKey_InvalidRequestException {}
class CloudKey_InvalidMethodException extends CloudKey_InvalidRequestException {}
class CloudKey_InvalidParamException extends CloudKey_InvalidRequestException {}

class CloudKey_ApplicationException extends CloudKey_RPCException {}
class CloudKey_NotFoundException extends CloudKey_ApplicationException {}
class CloudKey_ExistsException extends CloudKey_ApplicationException {}
class CloudKey_LimitExceededException extends CloudKey_ApplicationException {}

And when I try to instance my object in controller, Symfony returns this:

Fatal error: Class 'CD\DMBundle\Entity\Exception' not found in /var/www/carpediese/src/CD/DMBundle/Entity/CloudKey.php on line 513

I think Exception class is native PHP5+ class. How can I tell it to Symfony?

6
  • 1
    \Exception is natively defined. From the namespace docs, "Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules". Thus, there must be a problem inside the CloudKey_Exception file, probably a use statement. Can we see the code? Commented Feb 4, 2013 at 14:35
  • Is this the file you are referring to? If so, there doesn't appear to be any namespace references in there at all - have you modified it? Commented Feb 4, 2013 at 14:41
  • I add a slash before Exception [...] extends \Exception {} So this error go away. But another one show up : Fatal error: Class 'CloudKey_Api' not found in /var/www/carpediese/src/CD/DMBundle/Entity/CloudKey.php on line 46 Yes this is this file (DMCloud API). I add namespace CD\DMBundle\Entity; on the top of my file. Commented Feb 4, 2013 at 14:48
  • If you've had to edit this file, then make a pull request to the maintainer, so no-one else has to have the same problem. Commented Feb 4, 2013 at 14:52
  • I wont pull my changes, if you make natif PHP, you don't need to use namespace (not necessary), and you don't need to change file. If you use framework, you have to know how add external library in your framework. Thank all of you :) Commented Feb 4, 2013 at 15:05

1 Answer 1

2

Remember to properly set the use statements in files which use the Exception class.

EDIT:

When you refer to any class in PHP 5.3+ just below the namespace declaration you need to add which namespaces you are using for the referenced class (or use the whole namespace when referencing the class). So, if the Exception class you are using belongs to say someLibrary\ClouKey\Exceptions\ namespace you should either have

use someLibrary\CloudKey\Exceptions\Exception;

at the beginning of the file, just below namespace, or use the whole namespace when defining your new class:

class CloudKey_Exception extends someLibrary\CloudKey\Exceptions\Exception {}

EDIT 2:

In the class you are using the Exception is indeed the native PHP class so \Exception should be used. The error you get is generated by this part of the CloudKey class:

public function __get($name)
{
    if (!isset($this->objects[$name]))
    {
        $class = 'CloudKey_' . ucfirst($name);
        if (!class_exists($class))
        {
            $class = 'CloudKey_Api';
        }
        $this->objects[$name] = new $class($this->user_id, $this->api_key, $this->base_url, $this->cdn_url, $name, $this->proxy, $this->timeout);
        $this->objects[$name]->parent = $this;
    }

    return $this->objects[$name];
}

According to the documentation (http://php.net/manual/en/language.oop5.basic.php):

If a string containing the name of a class is used with new, a new instance of that class will be created. If the class is in a namespace, its fully qualified name must be used when doing this.

So you have to edit the quoted part of the code to use the whole namespace inside the string for the class name.

Sign up to request clarification or add additional context in comments.

3 Comments

What should that statement look like in this case?
I've edited the answer to make it more clear - the first version was a bit too concise ;)
Ah, I thought this library was extending core \Exception? The link to the code is in a comment of mine under the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.