Question

I am writing a method which can call any method from any class (this process is dynamic). In my method, I need to find out what type is the returned value, based on the returned value type,I will proceed on to the next step.

For example:

<?php
  function identifyReturnType($className, $methodName) {
       $result = $className->$methodName();
       //Here I need to find out the $result type
  }
?>

I have many classes where methods return bool, string, int etc. and there are a few methods which do not return anything, those methods set the values in object or the object has resource pointer :

<?php
    function getCategories() {
        $this->query("SELECT * FROM categories");
    }

    function getItems() {
        $this->query("SELECT * FROM items");
        $this->getValues();
    }
?>

PHP gettype($var) method finds out what is the value type but for this, my method must return a value. I have cases (as I explained above) where method just sets the query object.

Please share your ideas.

Thank you so much.

Was it helpful?

Solution

This really depends on your implementation. Some follow architecture where every function will return data as array. Even for query returned data is returned in small chunks of array. That is completely on how you optimize or write your script. Say you are getting all contacts and if you have say 10,000 contacts in DB and you return all in an array, thats a bad idea. Rather use pagination and return in small numbers if you want the function to return data as array.

I have had this issue, where we have a big web application written in PHP/Mysql. Over the time we have thousands of functions across different classes. Now we have to develop a REST API which will have different functionality. The main problem was we do not have used different functions to return query object, some to return array, some to return Boolean and so on. The API should return data as JSON. Now we have to choice use the existing code for different functionality or re-write new code for the API. The 2nd choice is more expensive so we are left with first choice. But the problem as I mentioned is far from over the methods will return different type and do we need to really write more codes to check which function is called and if the say function "xyz()" is called and we know its returning query object then loop through it generate array and then json. No thats a bad idea and will take a lot of effort and its better to write seperate code then.

So we follow the following approach.

Our api call looks like

www.api.oursite.com/api/v1/Resource/Method?param=....

Now we catch the Resource and Method where resource is a Class name and Method is a method name for that Class.

so we know we have to call Resource->Method()

Now we have a class called ResourceMethodMap.class.php and it contains the array as

static $resource_method_map = array(
    "Users"=>array(
"getUserInfo"=> // gets the user info 
array(
"return"=>"array",
"accessToken"=>true
)
),
....
...

)

So the API request processing code does something like

public function call_method($resource = "",$method=""){
    if($resource == "") $resource = $this->get_resource();
if($method == "") $method = $this->get_api_method();
if (class_exists($resource)) { 
  $resource_obj = new $resource();
      // Parse the method params as array
  $param_array = $this->parse_method_params($resource,$method);
      if(false !== $param_array){
  $result = call_user_func_array(array($resource_obj, $method), $param_array);
}else{
  $result = $resource_obj->$method() ;
}   
return   $this->process_return_data($resource,$method,$result,$resource_obj);                   
}else{
$this->setMessage("Invalid Resource");
return false ;
}

}

Here the function process_return_data() will do the returned data conversion as

function process_return_data($resource,$method,$ret_val,$resource_obj = NULL){ if(array_key_exists("return",ResourceMethodMap::$resource_method_map[$resource][$method])){ $return_type = ResourceMethodMap::$resource_method_map[$resource][$method]["return"]; $return_array= array(); switch($return_type){ case 'boolean': if(false === $ret_val){ return false ; }else{ if(is_array($ret_val)){ return $ret_val ; }elseif(true === $ret_val){ return $ret_val ; }else{ $return_array[] = $ret_val ; return $return_array ;
} } break; case 'array': return $ret_val ; break; } ..... } }

So Yes it completely on the developer how they want their data to be returned. The above example is just one real time scenario how we have implemented.

I have posted the complete code her http://codepad.org/MPY1gVed have look

OTHER TIPS

If i understood your question right you can do this by passing in an argument as a reference. Here's an example i made for you, if it is any help.

http://php.net/manual/en/language.references.pass.php

Another solution can be to return an array with both the return value and the type.

Do you real need a method to call other methods? You could just instantiate the class and call it manually

In adittion i would recommend checking like so:

if(is_callable($className, $methodName)){
      $className->$methodName();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top