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

发布于 2024-11-16 00:59:02 字数 1657 浏览 0 评论 0原文

我对类和函数还很陌生,所以我正在使用一个嵌入 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>";
    } }
    ?>

和输出 演示.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>

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/download/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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉宸 2024-11-23 00:59:02

如果 $height$width 仅与 embedSWF() 方法相关(我怀疑这一点,因为它看起来不像尺寸与 Media 实例 关联,但对于 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) {
        // ...
    }
}

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) {
        // ...
    }
}
夏末的微笑 2024-11-23 00:59:02

更改您的 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);
?>

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);
?>
神经暖 2024-11-23 00:59:02

您需要将宽度和高度作为参数传递给函数,然后

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

将函数更改为类似于

public function 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.

几度春秋 2024-11-23 00:59:02

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

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);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文