1

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.

2
  • It also might be a good idea to inject Request instead of hard coding it in constructor: $client = new \API\Client(new \API\Client\Request); Commented Oct 16, 2014 at 10:51
  • write in constructor require_once 'src/API/Client/Request.php'; above $this->_requester = new Client\Request(); line. Commented Oct 16, 2014 at 10:52

1 Answer 1

1

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.

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

1 Comment

I used an autoloader combined with a RecursiveDirectoryIteratorto get all the files autoloaded. Thanks for your help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.