dongrong7883 2010-05-27 10:21
浏览 19
已采纳

php中面向对象的问题

I have come up to issues while I'm trying to write some classes, here is an example: I have this class called TwitterGrub and I cant call it like that:

$c = new TwitterGrub();

$c->twitterDisplay();

here is the class itself:

<?php
class TwitterGrub{


function twitterCapture($user = 'username',$password = 'pass') {  


           $ch = curl_init("https://twitter.com/statuses/user_timeline.xml");  
           curl_setopt($ch, CURLOPT_HEADER, 1);  
           curl_setopt($ch,CURLOPT_TIMEOUT, 30);  
           curl_setopt($ch,CURLOPT_USERPWD,$user . ":" . $password);  
           curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  
           curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);  
           curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);  
           $result=curl_exec ($ch);  
           $data = strstr($result, '<?');  

           $xml = new SimpleXMLElement($data);  

      return $xml;  

}  


function twitterDisplay($twitNum = 2){
    $xml = $this::twitterCapture(); 


    for($i= 0; $i<$twitNum; $i++){ 
    echo   "<div class= 'curvebox'>".$xml->status[$i]->text."</div>";

    }
}

}

?>

The problem is that everytime I want to change the username or password I have to jump back to class definition and that makes things not modular... and in many ways it feels wrong. So the question is what would be the proper way to chance my username and password through the objects interface and then call twitterDisplay() method with the new data?Hope that makes sense. Thanks in advance

  • 写回答

3条回答 默认 最新

  • duangan9251 2010-05-27 10:44
    关注

    All the answer so far are correct and you should consider them, but I just want to show you, that you are already there:

    Your method twitterCapture takes a username and password as parameter, so make use of this and also define your twitterDisplay function with these parameters:

    function twitterDisplay($twitNum = 2, $user='default', $passwd='default'){
        $xml = $this->twitterCapture($user, $passwd);
    
        for($i= 0; $i<$twitNum; $i++){ 
            echo   "<div class= 'curvebox'>".$xml->status[$i]->text."</div>";    
        }
    }
    

    Then you can do:

    $c = new TwitterGrub();
    
    $c->twitterDisplay('foo', 'secret');
    //and
    $c->twitterDisplay('bar', 'secret2');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败