This is a small php which will read the youtube RSS feed. What I want to do is convert it into object oriented. Using class and function. I am new to object oriented, can some body show me a way how and where to start with? I have read the acticle from php dot net but still I am a bit confuse.

a small example will be appreciate
thank you for your help

<?php
$html = "";
$url = "http://gdata.youtube.com/feeds/api/users/Sindrannaras/uploads";
$xml = simplexml_load_file($url);
for($i = 0; $i < 10; $i++){
$author = $xml->entry[$i]->author->name;
$id = $xml->entry[$i]->id;
$id = str_replace("http://gdata.youtube.com/feeds/api/videos/","",$id);
$title = $xml->entry[$i]->title;
$content = $xml->entry[$i]->content;
$html .= "<div><h3>$title</h3>";

$html .= "  $id<br />    $content<br />";


$html .= "<hr /></div>";
}
echo $html;
?>

Recommended Answers

All 4 Replies

Hi,

I agree with the above response. You will have to understand it, and use it to your application.

Let me write a simple skeleton on what you have above..

 class SomeYtApi{
 ## define properties
 private $url;
 private $id;

 ## followed by the constructor
 function __construct($url, $id){
 $this->url = $url;
 $this->id = $id;
 }
 ## this is method of the class
 public function doSomethingWithUrlAndId(){
 if(!empty($this->url)&& (!empty($this->id)){
 ## do something with the valid url and id above here

 return output;
} ## end of method.
} ## end of class

## if needed be, we extend the class above by doing like this

class newApi extends SomeYtApi{
## define property unique to this class
private $unique;

## this is the constructor of this extended class. I want you to pay attention on what are the properties inside the parenthesis.
 public function __construct( $url, $id, $unique ) {

 ## since that the parent class already have these properties, we need to envoke parents constructor. This is very important in learning OOP in php you can this->this and this->that, but if you don't understand this pretty well, you will be completely lost even for years.

 parent::__construct( $url, $id );
 ## this is the only time we can define the propert of this class

 $this->unique = $unique;

 }

 public function getThisUnique(){

 ## do whatever

 }

 ## you can even extend the parent class one more time to do another things.

The beauty of oop is that you can instantiate the class several times, and still get the result is only dependent to whatever property has been supplied or provided.

For example, the above can be instantiated like so

 $objectOne = new SomeYtApi($url1,$id1);
 $objectTwo = new SomeYtApi($url2,$id2);
 ## how about the extended class
 $objecThree = new NewApi($url3.$id3,$unique);

OOP is something you need to practice every day, until you become confident writing codes in a setting like this. Typing codes in a textarea. Of course, no one is expecting you to get it write on the very first time, because even the so called experts always makes mistakes. In php, mistakes create errors, and errors makes you search for the solution. The solution gives you the additional knowledge and understanding, so that you can move an inch closer to the next errors awaiting ahead of you.

on the other hand you can start something like this. Beginners can do this by wrapping two function inside the curly bracket of the class. amazingly, it gives you the same result as in my first example.

 class SomeClass{

 function SomeFunction(){

 }

 function anotherFunction(){

 }
 }## end of class

To instantiate the above class we can do as simple as this

 $object = new SomeClass;
 $something = $object->SomeFunction();
 $anotherThing = $object->anotherFunction();

ok, here is a simple class to get you started.. all you need to do provide the script witht the valid youtube username

<?php

    class GetUserVideo{

        private $user;

        function __construct($user){

            $this->user = $user;
        }

        public function getVideo(){
            $xml = simplexml_load_file("http://gdata.youtube.com/feeds/api/users/".$this->user."/uploads");
            $data = array(); 
            for($i = 0; $i < 10; $i++){
             $author = $xml->entry[$i]->author->name;
             $id = $xml->entry[$i]->id;
             $id = str_replace("http://gdata.youtube.com/feeds/api/videos/","",$id);
                $data['title'] = (string)($xml->entry[$i]->title);
                $data['content'] = (string)($xml->entry[$i]->content);
                $data['id']  = (string)($id);
                $vidInfo[] = $data;

            }
            return $vidInfo;
        }
    }

  ## instatntiate the class and setting the user
  $items = new GetUserVideo("Sindrannaras");
  ## we show the result
  $show = $items->getVideo();
  foreach ($show as $item){
    echo "<p>".$item['id']."<br/>";
    echo $item['title']."<br/>";
    echo $item['content']."<br/></p>";
} 

By the way, youtube allows 25 items per query and per username. If you want to parse all of those included in the rss feeds, then you must modify this part of the class above..

change this

 private $user;

to this

private $user;
private $total;

then just change the method to this

 public function getVideo(){
 $xml = simplexml_load_file("http://gdata.youtube.com/feeds/api/users/".$this->user."/uploads");
$this->total = count($xml->entry);
    $data = array(); 
    for($i = 0; $i < $this->total; $i++) {
$author = $xml->entry[$i]->author->name;
$id = $xml->entry[$i]->id;
$id = str_replace("http://gdata.youtube.com/feeds/api/videos/","",$id);
$data['title'] = (string)($xml->entry[$i]->title);
$data['content'] = (string)($xml->entry[$i]->content);
$data['id'] = (string)($id);
$vidInfo[] = $data;
}
return $vidInfo;
}

if you want to show the thumbnails, you must change the codes below the instantiated class to this.

  foreach ($show as $item){
  echo "<p><img src='http://i.ytimg.com/vi/".$item['id']."/0.jpg' width='100' height='100'/><br/>";
  echo $item['id']."<br/>";
  echo $item['title']."<br/>";
  echo $item['content']."<br/></p>";
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.