PHP:通过包含的函数更改属性
我对类和函数还很陌生,所以我正在使用一个嵌入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果
$height
和$width
仅与embedSWF()
方法相关(我怀疑这一点,因为它看起来不像尺寸与Media
实例 关联,但对于embedSWF()
方法调用),您应该将它们与$swf
参数:调用方式如下所以:
编辑
一般来说,当属性表达或与当前实例(对象)的状态相关时,您应该向类添加属性。例如,
在上面,
Media
实例具有可以以不同格式输出的状态(具有尺寸的SWF)。即,该方法将内部状态转换为输出。或者,我们有一个没有状态的“类似实用程序”的类。相反,它拥有一组专门处理传递的数据(参数)的方法。例如,
If the
$height
and$width
are only relevant for theembedSWF()
method (and I suspect this as it does not look like the dimensions are associated with theMedia
instance but to theembedSWF()
method call), you should add them along with the$swf
argument:Called like so:
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.,
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.,
更改您的
embedSWF
函数以向其添加两个参数,分别为$width
和$height
,如下所示:然后,按如下方式调用它:
Change your
embedSWF
function to add two parameters to it, respectively$width
and$height
, like this:Then, call it like this:
您需要将宽度和高度作为参数传递给函数,然后
将函数更改为类似于
public function embedSWF ($swf, width, height)
并删除宽度和高度的启动你的职能。
您的宽度和高度当前超出了对象范围。
You need to pass the widht and height as parameters to your function like so
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.
将
$width
和$height
更改为对象属性,如下所示:用法:
Change
$width
and$height
to object attributes like so:Usage: