douliao8402 2011-11-11 02:27
浏览 84
已采纳

从php中的类调用函数

i am having a class in some functions in this class. all the functions have variables. the code is written below

<?php
class myclass{
    public function getresults{
        $url = 'http://www.slideshare.net/api/2/search_slideshows?q=google';
        echo $url;
        $ch=curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Your application name');
        $query = curl_exec($ch);
        $errorCode = curl_errno($ch); 
        curl_close($ch);
        $array = (array) simplexml_load_string($query);
        //echo '<pre>';
        //print_r($array);
        public $TotalResults;
        $TotalResults = $array['Meta']->TotalResults;
        echo "function is correct";
    }

}

when i call this fun

echo $obj1->TotalResults;

this gives error to me. please help me and modify my code.

  • 写回答

2条回答 默认 最新

  • douchun1961 2011-11-11 02:32
    关注

    You incorrectly used member variables:

    class myclass{
        public $TotalResults; // <-- added member variable
        public function getresults{
            $url = 'http://www.slideshare.net/api/2/search_slideshows?q=google';
            echo $url;
            $ch=curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Your application name');
            $query = curl_exec($ch);
            $errorCode = curl_errno($ch); 
            curl_close($ch);
            $array = (array) simplexml_load_string($query);
            //echo '<pre>';
            //print_r($array);
            $this->TotalResults = $array['Meta']->TotalResults; // <-- corrected
            echo "function is correct";
        }
    }
    

    and after you do $obj->getresults() (where $obj must be instantiated by $obj = new myclass(); before), the $obj->TotalResults should contain what you wanted.

    Did it help?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?