Ada*_*m P 4 php oop object amazon-web-services web
我有这个类的问题,特别是最后2个函数callApi($query)和objectToArray($d).这些目标是从Amazon API返回的对象返回一个数组.
我认为问题在于从另一个函数调用一个函数:
$arrayResponse=$this->objectToArray($response);
即使我在var_dump时这样做,我$arrayResponse仍然将它作为一个对象,并且不能执行特定于数组的操作(呃!;)).我objectToArray()在这个网站和亚马逊图书馆找到了.
当我单独测试它时它工作正常但在将它放入项目后我仍然得到了对象.
是我只是以错误的方式调用函数,所以它不转换它?
先感谢您,
亚当
class Amazon extends Source implements ICallsApi{
    function __construct(){
        $this->impact = 55;
        $this->side = "supply";
        echo "<p>I am Amazon</p>";
        echo "<p>and my impact = ". $this->impact."</p>";
    }
    private function FormulateQuery($query){
                echo "Formulate query for Amazon API and return it";
    }
    //Returns the array records from amazon of a given keyword      
    public function callApi($query)
            {
            $client = new AmazonECS('dataRemoved', 'dataRemoved', 'co.uk', 'dataRemoved');
            $response  = $client->category('Books')->search($query);
            //var_dump($response);
            //echo "<pre>";
            //print_r($response);
            //echo "</pre>";
            $arrayResponse=$this->objectToArray($response);
            var_dump($arrayResponse);
            //echo "<pre>";
            //print_r($arrayResponse);
            //echo "</pre>";
                echo "code returns an array of results";
            //  return $arrayResponse;
            }  //objectToArray($d) found online
        public  function objectToArray($d) {
                if (is_object($d)) {
                    // Gets the properties of the given object
                    // with get_object_vars function
                    $this->d = get_object_vars($d);
                }
                if (is_array($d)) {
                    /*
                    * Return array converted to object
                    * Using __FUNCTION__ (Magic constant)
                    * for recursive call
                    */
                    return array_map(__FUNCTION__, $d);
                }
                else {
                    // Return array
                    return $d;
                }
            }
}
好的,修好了.
供将来参考:
将整个std Object转换为数组的工作函数如下所示:
public function objectToArray($d) {
                if (is_object($d)) {
                    // Gets the properties of the given object
                    // with get_object_vars function
                    $d = get_object_vars($d);
                }
                if (is_array($d)) {
                    /*
                    * Return array converted to object
                    * Using __FUNCTION__ (Magic constant)
                    * for recursive call
                    */
                    return array_map(array($this, 'objectToArray'), $d);
                //$this->d = get_object_vars($d);
                }
                else {
                    // Return array
                    return $d;
                }
            }
谢谢所有贡献者,
亚当