An issue on object orientation in 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);
开发者_Python百科 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
I suggest:
$twitter = new TwitterGrub('myUser', 'myPass');
echo $twitter->twitterCapture(); // etc..
<?php
class TwitterGrub{
private $user;
private $password;
function __construct($user, $password) {
$this->user = $user;
$this->password = $password;
}
function setUser($user) {
$this->user = $user;
}
// same for password
function twitterCapture() {
$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,$this->user . ":" . $this->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(); // DONT USE :: here!
for($i= 0; $i<$twitNum; $i++){
echo "<div class= 'curvebox'>".$xml->status[$i]->text."</div>";
}
}
Store the user and password as member variables, and pass them to the constructor:
class TwitterGrub
{
private $_username;
private $_password;
public function __construct($username, $password)
{
$this->_username = $username;
$this->_password = $password;
}
// rest of the code, use $this->_username and $this->_password
}
Construct with:
$c = new TwitterGrub('user', 'pass');
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');
精彩评论