3
\$\begingroup\$
class Youtube
{
    private $url = null;
    private $id;

    public function __construct($url)
    {
        $this->url = $url;
        $this->parseUrl($url);
    }

    protected function parseUrl($url)
    {

        if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) {
            $this->id = $match[1];
        }
        else
        {
            throw new exception('Cannot find an ID!');
        }

    }


    public function getVideo($width='560', $height='315')
    {
        $video = <<<EOF
        <iframe width="$width" height="$height" src="//www.youtube-nocookie.com/embed/$this->id" frameborder="0" allowfullscreen></iframe>
EOF;

        return $video;
    }


}

$sistar = new Youtube('http://www.youtube.com/watch?v=JtVhwsACgTw');
print $sistar->getVideo();

$twoneone = new Youtube('http://www.youtube.com/watch?v=LUrUPzLm5SI');
print $twoneone->getVideo();

This is my first object-oriented script in PHP (I hope it's not too bad in general). I know I should do more validation and commenting within the code. Do you have any suggestions for improving the quality of the code?

\$\endgroup\$
0

4 Answers 4

3
\$\begingroup\$

Pinoniq has the right idea, which can be taken a bit further:

VideoFactory $vf = new VideoFactory();
Video $video = $vf->createVideo( "http://youtube.com/..." );
$video->setDimensions( 640, 480 );
$video->embed();

Here, the VideoFactory can parse a URL. The createVideo method would return a subclass of Video, which is a YouTubeVideo. However, the implementation need not be dependent on YouTube. This allows you to also write:

VideoFactory $vf = new VideoFactory();
Video $video = $vf->createVideo( "http://vimeo.com/..." );
$video->setDimensions( 640, 480 );
$video->embed();

You'll note that the only item that changed is the URL, but both video formats can be embedded.

\$\endgroup\$
6
  • \$\begingroup\$ Is this javascript? \$\endgroup\$
    – Naftali
    Aug 28, 2013 at 15:32
  • \$\begingroup\$ @Neal: Whoops! It was in Java, but now is in PHP. I should have wrote it in Haxe. ;-) \$\endgroup\$ Aug 28, 2013 at 15:59
  • \$\begingroup\$ Still not PHP... \$\endgroup\$
    – Naftali
    Aug 28, 2013 at 16:26
  • \$\begingroup\$ This site is not a wiki, and whoever told you that was wrong... \$\endgroup\$
    – Naftali
    Aug 28, 2013 at 18:30
  • 2
    \$\begingroup\$ @Neal and Dave: It is possible to edit other users' posts, but only for users with at least 1000 reputation points. So Neal wouldn't be able to edit this answer, but other users with higher reputation can. Either way pointing out mistakes through comments is perfectly fine of course. I hope this clarified things. \$\endgroup\$
    – sepp2k
    Aug 28, 2013 at 21:58
1
\$\begingroup\$

Personally I don't find this code usefull. I would rather go with a YoutubeVideo class and a YoutubeFactory that returns YoutubeVideo objects.

The YoutubeVideo object then has some functions to access data from the YoutubeVideo (length,...) and present that Data. i.e. as an iframe. I would then call that method toIframe($widh, $height); or something along those lines

The YoutubeFactory would parse the url and fill the youtubeVideo with all the information it needs.

This way you have seperation of concern. YoutubeFActory handles all the business logic where as YoutubeVideo is simply a container to represent the video.

Now you simply wrote class {} around a controller with some procedural functions.

Using the code would look something like this:

$ytbF = new YoutubeFActory();

$myVideo = $ytbF->createFromUrl('http://www.youtube.com/watch?v=JtVhwsACgTw');
$mySecondVideo = $ytbF->createFromId('JtVhwsACgTw');

and then in your template you would present the video:

<somehtml>
    <?php print $myVideo->toIframe(500,400); ?>
</somehtml>
\$\endgroup\$
0
\$\begingroup\$

I would probably put the $width and $height into the properties and not hard-coded into the method. That way you can change them as needed, instead of editing the class itself.

class Youtube {
    private $width = 123; // default value, if needed 
    private $height = 456; // default value, if needed 

    // Ctor and stuff...

    public function getVideo($width=null, $height=null) {
        // Code
    }
}

Then you can change the dimensions on the fly:

$video = new Youtube($url);
$video->getVideo(560, 315);
\$\endgroup\$
0
\$\begingroup\$

The constructor here is taking url as parameter,and it is declared as class level instance variable, so you will have access to this variable inside parseUrl method without passing this as an argument to the method

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy