在php中从类调用函数

在php中从类调用函数,php,function,Php,Function,我在这门课的一些函数中有一个类。所有函数都有变量。代码写在下面 <?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, $ur

我在这门课的一些函数中有一个类。所有函数都有变量。代码写在下面

<?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";
    }

}

这给了我错误。请帮助我并修改我的代码。

您错误地使用了成员变量:

<?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";
  }
}

$obj = new myclass;
echo $obj1->getresults();
class-myclass{
公共$TotalResults;//TotalResults=$array['Meta']->TotalResults;//getresults()
(其中,
$obj
必须由
$obj=new myclass();
之前实例化),
$obj->TotalResults
应该包含您想要的内容

它有用吗?

类似这样的东西:


我们必须猜测错误是什么吗?@rajzana:给我们更多关于错误的细节-你的代码中有一些问题,但它们可能不是全部,也不是最重要的问题。
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";
    }
}
<?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";
  }
}

$obj = new myclass;
echo $obj1->getresults();