Php nesne yönelimi hakkında bir sorun

3 Cevap 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();

Burada sınıf kendisidir:

<?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 Cevap

Tüm cevap şimdiye kadar doğru ve bunları düşünmelisiniz, ama ben sadece zaten orada olduğunu size göstermek istiyorum:

Sizin yöntemi twitterCapture parametre olarak bir kullanıcı adı ve şifre alır, yani make use of this ve bu parametreler ile twitterDisplay işlevi tanımlayın:

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

Sonra yapabilirsiniz:

$c = new TwitterGrub();

$c->twitterDisplay('foo', 'secret');
//and
$c->twitterDisplay('bar', 'secret2');

Öneririz:

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

Üye değişkenleri olarak kullanıcı ve parolayı depolamak ve kurucusuna onları geçmek:

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
}

Ile Construct:

$c = new TwitterGrub('user', 'pass');