0

I'm trying to create a class that uses cURL to make requests to the Twitter API. I've done some research on object oriented programming in PHP, and I can't quite figure out how this should work. The following code returns NULL:

 <?php

    class twitter {

      public function curlQuery($url) {
      $ch = curl_init();

      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

      $json = curl_exec($ch);

      curl_close($ch);

      $array = json_decode($json);
      return var_dump($array);
      }
}

$object1 = new twitter;
$object1->url = "http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=twitterapi&count=2";
$object1->curlQuery($object1->url);

 ?>

Also, I'm a little unsure when to use $this->variable vs $variable in a PHP class. Shouldn't all variables mentioned in an class be referenced as $this->variable? Why would you ever not want to reference the variable of the current object?

3
  • I tested your code its working. Commented Jun 9, 2012 at 5:25
  • You are simply wrapping a function in a class. There is nothing to be aware of here in terms of OOP. Commented Jun 9, 2012 at 5:27
  • You can always var_dump $json to see if you're actually getting returned data then you'll have a better idea of the culprit. Commented Jun 9, 2012 at 5:27

2 Answers 2

5

Well, why are you storing the url in the object, then passing that variable as a parameter to a method of that same object? Seems pointless, because then you could do somethign like:

$object1->url = 'blahblahblah';
$object1->curlQuery(); // <---note, no parameter

and then in the curlQuery method:

curl_setopt($ch, CURLOPT_URL, $this->url);

As for $this->variable v.s. $variable in PHP, $this->variable makes that variable a member of the object. It'll be available to all the methods in the object. $variable by itself will simply be a local variable within a single method. When that method returns, the local variable is destroyed and is no longer available to other methods.

In short, use $this->variable for things you need persisted within the object and across multiple method calls. Use $variable for temporary storage within a single method.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. Cleaned up the class code to reference the current object instead of storing the url in the object and passing it in as a parameter in the object's method. Works perfect. And great explanation of $this->variable vs $variable. That really cleared things up for me.
2

When the variables are defined as properties of the class, while using them in the member functions you should use the $this->variablename. But this doesnt mean : all variables mentioned in an class be referenced as $this->variable

You may use local variables inside the member functions in the other way. which will have scope inside the perticular function defined.

like

class twitter 
{
   var $apikey;

   function displayKey()
   {
     $k = 1;
     $this->apikey += $k;
     echo $this->apikey;
   }

 }

for referencing the apikey we use $this->apikey and for the local varibles inside the function we use the other way around.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.