Jump to content
Larry Ullman's Book Forums

__Construct() Is Return True--> Is It True?

Recommended Posts

public function __construct() {
$this->verb = $_SERVER['REQUEST_METHOD'];
$this->url_elements = explode('/', $_SERVER['PATH_INFO']);
        // initialise json as default format
        $this->format = 'json';
        if(isset($this->parameters['format'])) {
            $this->format = $this->parameters['format'];
        return true;

Hi ,


I remember that ___construct() didn't return any values..


On one of the tutorials on REst I have seen the code for creating a REst Server..

Can anyone please explain this?. How can they used it and any side effects?


Source of the code: http://www.lornajane.net/posts/2012/building-a-restful-php-server-understanding-the-request


Thanks in advance.


Link to comment
Share on other sites

If you read some of the comments, you'll see that the blog post has some inaccuracies. To cut it short, this is not really good code for you to learn from. You are absolutely right about return values in a constructor - it doesn't work.


The general principle of Rest is really good and one you should stick with. What it basically does is allowing the client to utilize any programming language to get a response from your server. I could be coding Ruby, PHP, Java, Python, whatever, and still be able to use something like a cURL-request to send requests and get responses from your server. :)


Good luck and ask if you have other questions.

Link to comment
Share on other sites

Hi Antonio,


I am working on REst API called tonic using mongoDB..


I am able to connect to db outside the class..

   	$con = new MongoClient();
        $db = $con->codesnippets;
        $collection = $db->snippets;

        //fetch a record
        $snippet = $collection->findOne(array("id" => 1));


When I put this in Rest API method its not working..and error is ....nothing


namespace Tyrell;

use Tonic\Resource,

 * This is a
 * The @uri annotation routes requests that match that URL to this resource. Multiple
 * annotations allow this resource to match multiple URLs.
 * @uri /snippets/:idhash
class Snippet extends Resource

     * @method GET
     * @provides application/json
    function display() {

        // Connect to mongodb
        $con = new \MongoClient();
        $db = $con->codesnippets;
        $collection = $db->snippets;

        //print $this->idhash;

        //fetch record based on id
        $snippet = $collection->findOne(array("id" => $this->idhash));

            $snippetArray = array('id' => $snippet["id"],
                                  'title' => $snippet["title"],
                                  'description' => $snippet["description"],
                                  'links' => $snippet['links'],
                                  'files' => $snippet['files'],
                                  'dateCreated' => $snippet["date_created"],
                                  'lastModified' => $snippet["last_modified"],
                                  'views' => $snippet["views"]);

        return json_encode($snippetArray);

        //$ds = $this->container['dataStore'];
        //return json_encode($ds->fetch($this->id));


Is it conflicting with name space ?


can you please help me?.. I don't have much namespace knowledge.


Thanks in advance,


Link to comment
Share on other sites

Thanks for your prompt response.


I am not getting errors. I am suspicious about there is no db connections from API?



I checked the issues and the author is saying we need to write our own db wrapper. How do I do that?



it seems I need to have more oop. Please show me some light. Bit scared of namespaces, adapters etc ..nail biting




Link to comment
Share on other sites

Hi ,


I made Tonic Rest application  working using RedBean ORM wrapper without class dependency "Pimple".

The real credit goes to "Slim" Rest Framework.

Now I am able to work with both REST API's.

Mean time I encountered with the concept/hands on CURL, JSON to really work with Total REST Cycle.


I am happy to help anyone on this. REST Cycle.


Its all started with this article..



Next to Slim..





Next to ..



in the middle I got to know about..Pimple..





Finally End up

Redbean ORM, CURL, 




The Book for PHP REST..

PHP Web Services on OReilly.com(Just 124 pages..)



Thanks for your support..

sharma chelluri

  • Upvote 1
Link to comment
Share on other sites


  • Create New...