Jump to content

Notice: Undefined property: RestRequest::$getMethod


hackalive

Recommended Posts

Hi guys,

 

When using http://www.gen-x-design.com/archives/create-a-rest-api-with-php/

 

And implementing this part:

    $data = RestUtils::processRequest();  
      
    switch($data->getMethod)  
    {  
        // this is a request for all users, not one in particular  
        case 'get':  
            $user_list = getUserList(); // assume this returns an array  
      
            if($data->getHttpAccept == 'json')  
            {  
                RestUtils::sendResponse(200, json_encode($user_list), 'application/json');  
            }  
            else if ($data->getHttpAccept == 'xml')  
            {  
                // using the XML_SERIALIZER Pear Package  
                $options = array  
                (  
                    'indent' => '     ',  
                    'addDecl' => false,  
                    'rootName' => $fc->getAction(),  
                    XML_SERIALIZER_OPTION_RETURN_RESULT => true  
                );  
                $serializer = new XML_Serializer($options);  
      
                RestUtils::sendResponse(200, $serializer->serialize($user_list), 'application/xml');  
            }  
      
            break;  
        // new user create  
        case 'post':  
            $user = new User();  
            $user->setFirstName($data->getData()->first_name);  // just for example, this should be done cleaner  
            // and so on...  
            $user->save();  
      
            // just send the new ID as the body  
            RestUtils::sendResponse(201, $user->getId());  
            break;  
    }  

 

I get an error;

Notice: Undefined property: RestRequest::$getMethod

 

 

Any ideas why or how I can fix it?

 

Cheers in advance

 

There's a bug in the posted code (that you're using). Perhaps you should let the author know.

 

$data->getMethod

 

That's looking for a property (a public member variable) called getMethod in the object to which $data is pointing. There is no such property. There *is*, however, a *method* (function) called getMethod, and to call a method on an object you need to provide parentheses, like this:

 

$data-getMethod()

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.