1

I am having trouble calling a specific method from another class in my app. I have a class, Rest, that determines various settings, etc. about a particular request received by the server and creates a Rest object with the properties of the request. The Rest class may then call any given method in a separate class to fulfill the request. The problem is that the other class needs to call methods in the Rest class to send a response, etc.

How can this be possible? Here's a blueprint of my current setup:

class Rest {
 public $controller = null;
 public $method = null;
 public $accept = null;
 public function __construct() {
 // Determine the type of request, etc. and set properties
 $this->controller = "Users";
 $this->method = "index";
 $this->accept = "json";
 // Load the requested controller
 $obj = new $this->controller;
 call_user_func(array($obj, $this->method));
 }
 public function send_response($response) {
 if ( $this->accept == "json" ) {
 echo json_encode($response);
 }
 }
}

The controller class:

class Users {
 public static function index() {
 // Do stuff
 Rest::send_response($response_data);
 }
}

This results in receiving a fatal error in the send_response method: Using $this when not in object context

What's the better way to do this without sacrificing the current workflow.

asked Oct 15, 2011 at 19:05

3 Answers 3

4

You can create a Rest instance in User:

public static function index() {
 // Do stuff
 $rest = new Rest;
 $rest::send_response($response_data);
}

You could also change Rest to be a singleton and call an instance of it, but beware of this antipattern.

answered Oct 15, 2011 at 19:15

3 Comments

I would like to avoid creating another instance of Rest. I already have a Rest instance--the one that called index() in Users. How do I access that instance again?
Pass the existing Rest object as a parameter to User::index() (or the User constructor, or a User::setRest() method etc.), then call $rest->send_response($response_data);, perhaps.
Alternatively, use a singleton.
2

You need to create an instance first.

class Users {
 public static function index() {
 // Do stuff
 $rest = new Rest();
 $rest->send_response($response_data);
 }
}
answered Oct 15, 2011 at 19:12

Comments

0

You don't call send_response() in an object context, as the error message says.

Either you create an instance and call everything on that instance (IMHO the right way), or you do everything statically, including the constructor (you may want to have a intialization method instead) and the properties.

answered Oct 15, 2011 at 19:11

2 Comments

I'm not calling send_response in an object context. The problem is not calling send_response but accessing the object properties of the original Rest object from within send_response.
That's what I said. You don't call send_response() in an object context. Do it or make your send_response() and the properties static and change the constructor to an initializing function :) But IMHO the right solution would be to create an instance and call it in object context.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.