PHP:通过包含的函数更改属性

时间:2011-06-17 08:15:26

标签: php class function flash embed

我对类和函数都很陌生,所以我正在使用一个简单的脚本来嵌入Youtube视频和本地swf。它工作正常,但我无法更改swf中的$ width和$ height属性。如果我在函数内部创建它们,我必须将值设置为“0”,因此不会被外部值更新。它将保持为0。

有两个文件:

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/download/index.cgi?P1_Prod_Version=ShockwaveFlash'> 
</OBJECT>";
    } }
    ?>

输出 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>

4 个答案:

答案 0 :(得分:2)

如果$height$width仅与embedSWF()方法相关(我怀疑这一点,因为它看起来不像Media < em> instance 但是对于embedSWF() 方法调用),您应该将它们与$swf参数一起添加:

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

这样称呼:

<?php 
$media = new Media();
 $swf = "test.swf";
$media-> embedSWF($swf, 480, 360);
?>

修改
通常,在表达或与当前实例(对象)状态相关时,应该向类添加属性。如,

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() {
        // ...
    }
}

在上文中,Media实例具有状态(具有维度的SWF),可以以不同格式输出。即,该方法将内部状态转换为输出。

或者,我们有一个没有状态的“类似实用程序”的类。相反,它拥有一组专门用于传递数据(参数)的方法。如,

public class Media {
    public embedSWF($swf, $height, $width) {
        // ...
    }

    public embedYT($code) {
        // ...
    }
}

答案 1 :(得分:1)

您需要将widht和height作为参数传递给您的函数,如此

$height = "360";
$width = "480";
$media-> embedSWF($swf, width, height);

然后将您的功能更改为

public function embedSWF ($swf, width, height)

并删除您在函数中启动的宽度和高度。

您的宽度和高度目前超出了对象范围。

答案 2 :(得分:1)

更改您的embedSWF功能,分别为其添加两个参数$width$height,如下所示:

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

然后,这样称呼它:

<?php 
$media = new Media();
 $swf = "test.swf";
 $height = "360";
 $width = "480";
$media-> embedSWF($swf, $width, $height);
?>

答案 3 :(得分:0)

$width$height更改为对象属性,如下所示:

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

用法:

<?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);
?>