PHP: Changing atributes through included function
I'm pretty new to classes and functions, so I'm working with a simple script which embeds a Youtube video and a local swf. It works fine, but I'm unable to change the $width and $height attributes in the swf. If I create them inside the function I have to set the value to '0', thus not being updated by the external value. It will remain as 0.
There are two files:
classMedia.php
<?php
/*Gabriel*/
class Media {
public function embedYT($code){
echo "<iframe width='560' height='349' src='http://www.youtube.com/embed/".$code." ' frameborder='0' allowfullscreen></iframe>";
}
public function embedSWF ($swf){
$width='0';
$height='0';
echo "<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0' ID=objects WIDTH=460 HEIGHT=80>
<PARAM NAME=movie VALUE=".$swf." '>
<EMBED src=".$swf." ' WIDTH=".$width." HEIGHT=".$height." TYPE='application/x-shockwave-flash' PLUGINSPAGE='http://www.macromedia.com/shockwave/开发者_StackOverflowdownload/index.cgi?P1_Prod_Version=ShockwaveFlash'>
</OBJECT>";
} }
?>
And the output demo.php
<?php include "classMedia.php"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$media = new Media();
$code = "XSGBVzeBUbk";
$media-> embedYT($code);
?>
<?php
$media = new Media();
$swf = "test.swf";
$height = "360";
$width = "480";
$media-> embedSWF($swf);
?>
</body>
</html>
If the $height
and $width
are only relevant for the embedSWF()
method (and I suspect this as it does not look like the dimensions are associated with the Media
instance but to the embedSWF()
method call), you should add them along with the $swf
argument:
public function embedSWF ($swf, $width = 0, $height = 0) {
echo "<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0' ID=objects WIDTH=460 HEIGHT=80><PARAM NAME=movie VALUE=".$swf." '><EMBED src=".$swf." ' WIDTH=".$width." HEIGHT=".$height." TYPE='application/x-shockwave-flash' PLUGINSPAGE='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash'></OBJECT>";
}
Called like so:
<?php
$media = new Media();
$swf = "test.swf";
$media-> embedSWF($swf, 480, 360);
?>
EDIT
In general, you should add attributes to the class when they express or relate to the state of the current instance (object). E.g.,
public class Media {
private $swf;
private $height;
private $width;
public __construct($swf, $height, $width) {
$this->swf = $swf;
$this->height = $height;
$this->width = $width;
}
public getEmbedCode() {
// ...
}
public getIframeCode() {
// ...
}
}
In the above, a Media
instance has a state (an SWF with dimension) which can be output in different formats. I.e., the method transforms the internal state to an output.
Alternatively, we have a "utility-like" class with no state. Instead it holds a set of methods which works exclusively on passed data (parameters). E.g.,
public class Media {
public embedSWF($swf, $height, $width) {
// ...
}
public embedYT($code) {
// ...
}
}
Change your embedSWF
function to add two parameters to it, respectively $width
and $height
, like this:
public function embedSWF ($swf, $width = '0', $height = '0'){
echo "<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0' ID=objects WIDTH=460 HEIGHT=80>
<PARAM NAME=movie VALUE=".$swf." '>
<EMBED src=".$swf." ' WIDTH=".$width." HEIGHT=".$height." TYPE='application/x-shockwave-flash' PLUGINSPAGE='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash'>
</OBJECT>";
}
Then, call it like this:
<?php
$media = new Media();
$swf = "test.swf";
$height = "360";
$width = "480";
$media-> embedSWF($swf, $width, $height);
?>
You need to pass the widht and height as parameters to your function like so
$height = "360";
$width = "480";
$media-> embedSWF($swf, width, height);
then change your function to look like this
public function embedSWF ($swf, width, height)
and remove your initiation of width and height in your function.
Your width and height are currently out of the objects scope.
Change $width
and $height
to object attributes like so:
<?php
/*Gabriel*/
class Media {
public $width = 0;
public $height = 0;
public function embedYT($code){
echo "<iframe width='560' height='349' src='http://www.youtube.com/embed/".$code." ' frameborder='0' allowfullscreen></iframe>";
}
public function embedSWF ($swf){
echo "<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0' ID=objects WIDTH=460 HEIGHT=80>
<PARAM NAME=movie VALUE=".$swf." '>
<EMBED src=".$swf." ' WIDTH=".$this->width." HEIGHT=".$this->height." TYPE='application/x-shockwave-flash' PLUGINSPAGE='http://www.macromedia.com/shockwave /download/index.cgi?P1_Prod_Version=ShockwaveFlash'>
</OBJECT>";
}
}
?>
Usage:
<?php
$media = new Media();
$code = "XSGBVzeBUbk";
$media-> embedYT($code);
?>
<?php
$media = new Media();
$swf = "test.swf";
$media->height = 360;
$media->width = 480;
$media-> embedSWF($swf);
?>
精彩评论