php中面向对象的问题(An issue on object orientation in php)
我在尝试编写一些类时遇到了问题,这里有一个例子:我有一个名为TwitterGrub的类,我不能这样称呼它:
$c = new TwitterGrub(); $c->twitterDisplay();
这是班级本身:
<?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>"; } } } ?>
问题是,每当我想要更改用户名或密码时,我必须跳回到类定义,这使得事情不是模块化的......在许多方面,它感觉不对。 那么问题是通过对象接口获取用户名和密码然后用新数据调用twitterDisplay()方法的正确方法是什么?希望这是有道理的。 提前致谢
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
原文:https://stackoverflow.com/questions/2920243