2011-11-11 116 views
-4

我在這個類的一些函數中有一個類。所有的功能都有變量。代碼如下從一個類中調用函數php

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

} 

寫的時候我把這種樂趣

echo $obj1->TotalResults; 

這給了錯誤給我。請幫助我並修改我的代碼。

+7

我們有猜測錯誤的是? –

+0

@rajzana:給我們提供關於錯誤的更多細節 - 代碼中存在一些問題,但它們可能不是全部,也可能不是最重要的。 – Tadeck

回答

1

您正確使用成員變量:

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

和你做$obj->getresults()(其中$obj必須$obj = new myclass();之前被實例化)之後,$obj->TotalResults應該包含你想要的東西。

幫助?

0

事情是這樣的:

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