从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.

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?

Something like this :

<?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();