Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

We are updating our codebase to use the namespace functionality provided in PHP-5.3+

Previously, our files all lived happily on the webserver in /htdocs/php but now we are separating our scripts, and putting our PHP libraries in /htdocs/php/lib/

In /htdocs/php/lib/ we have a file called Jira.php which we have given a namespace to:

<?php
# Define a namespace for this library
namespace Jira;

function create_client($jira_url, $options)
{
    global $client;

    try
    {
            $client = new SoapClient($jira_url, $options);
    }
    catch (Exception $error)
    {
            echo $error -> getMessage() . "<br/><p style='color:red'> Could not connect to Jira </p>";
    }
}
?>

However, when we try and call this function from a script in /htdocs/php, we are getting the class not found error:

PHP Fatal error:  Class 'Jira\SoapClient' not found

This is failing when we try creating the SoapClient object.

I have verified that I have the php-soap package installed in /usr/share/php/SOAP/ and php_info(); is showing that it is enabled etc.

So presumably this is a problem with the namespace convention. How do we include that class without it throwing an error?

Regards, ns

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

SoapClient is probably assigned to the global namespace.

In your script the current namespace is Jira.

Try using:

$client = new \SoapClient($jira_url, $options); 
share|improve this answer
    
Genius.Thanks very much ++ –  nonshatter Feb 17 '12 at 11:22
    
Forgot to add, the \ before SoapClient means the globla namespace. So, if you have another class in the modules namespace, use \modules\myclass to reference it. You should also look into the use statement (php.net/manual/en/language.namespaces.importing.php) as well. –  F21 Feb 17 '12 at 11:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.