Dan Hanly
Dan Hanly

Reputation: 7839

Class not found on namespace

I'm creating an API wrapper at the moment, and I decided to create an index.php which loads the Client, just for testing/debugging purposes.

Here's my index.php

require_once("src/API/Client.php");

$client = new \API\Client();

Here's my API\Client

namespace API;

class Client
{
    public function __construct()
    {
        $this->_requester = new Client\Request();
        return $this;
    }
}

The error I'm getting is that API\Client\Request is not found in the Client.php

It does exist, and can be viewed below:

namespace API\Client

class Request
{
    protected $_requestUrl;

    public function __construct()
    {
        return $this;
    }    
}

This is my first foray into making an application that has fully namespaced classes, so I'd appreciate your help in getting this working.

Upvotes: 1

Views: 153

Answers (1)

MrCode
MrCode

Reputation: 64526

You're missing the require_once statement to include the script that contains the Request class definition.

require_once("src/API/Client.php");
require_once("src/API/Client/Request.php"); // <-- or whatever the filename is

I recommend using an autoloader which means you don't need any include statements. For example this PSR-0 autoloader.

Also, your use of the return statement in the constructors serves no purpose. Constructors can't return values.

Upvotes: 1

Related Questions