使用PHP类时出错<b>注意</b>:未定义的属性

时间:2011-03-30 18:44:03

标签: php actionscript-3 class

我仍然是PHP的新手。我有一个flash文件,它将一些变量传递给php脚本,并创建一组jpg文件的zip文件。当我在一个php文件中包含所有代码时,一切正常。现在我想将脚本分成两个文件。一个获取变量的php文件和一个实际完成创建文件工作的类。但是,当我运行它时,我收到此错误。

<br />
<b>Notice</b>:  Undefined property: ZipTestClass::$FileName.zip in <b>F:\Web Page      mcwphoto43\TestZip\Array\ZipTestClass.php</b> on line <b>15</b><br />
<br />

这是我的原始代码,工作得很好

<?PHP
//stores the URLvariables into variables that php can use
$img1 = $_GET['Image1']; 
$img2 = $_GET['Image2']; 
$img3= $_GET['Image3'];
$zipName= $_GET['Name'];

// create object
$zip = new ZipArchive();

// open archive 
if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}

// list of files to add
$fileList = array(
'ZipTest/' . $img1,
'ZipTest/' . $img2,
'ZipTest/' . $img3
);

// add files
foreach ($fileList as $f) {
$zip->addFile($f) or die ("ERROR: Could not add file: $f");  
}

// close and save archive
$zip->close();
echo "Archive created successfully.";   
?> 

以下是我的新代码的第一部分,它引入变量并调用类

<?PHP

//stores the URLvariables into variables that php can use
$Img1 = $_GET['Image1']; 
$Img2 = $_GET['Image2']; 
$Img3= $_GET['Image3'];
$zipName= $_GET['Name'];
//$RandVariable = $_GET['randString'];

echo "Variables $Img1, $Img2, $Img3, $zipName were accepted."; 

//Brings in ZipTestClass, creates an instance of the class as a variable, assgns values to the class
include "ZipTestClass.php";

$OrderInfo = new ZipTestClass;

$OrderInfo->img1 = "$Img1";
$OrderInfo->img2 = "$Img2";
$OrderInfo->img3 = "$Img3";
$OrderInfo->zipName = "$zipName";
$OrderInfo->CreateZip($Img1,$Img2,$Img3,$zipName);

?>

这是课程

<?PHP
class ZipTestClass{

//Receivers of the URLvariables needed to create the sip file based on the customers     order
var $img1;
var $img2;
var $img3;
var $zipName;
var $zip;

public function CreateZip($img1, $img2, $img3, $zipName)
{
    // create object
    $zip = new ZipArchive();
    $fileArchive = $this->$zipName;

    // open archive 
    if ($zip->open('Work', ZIPARCHIVE::CREATE) !== TRUE) 
    {
        die ("Could not open archive");
    }


    // list of files to add
    $fileList = array('ZipTest/' . $this->$img1, 'ZipTest/' . $this->$img2,     'ZipTest/' . $this->$img3);

    // add files
    foreach ($fileList as $f) 
    {
        $zip->addFile($f) or die ("ERROR: Could not add file: $f");   
    }

    // close and save archive
    $zip->close();
    echo "Archive created successfully."; 
}
}
?> 

非常感谢任何帮助

3 个答案:

答案 0 :(得分:7)

简单:将$fileArchive = $this->$zipName;更改为$fileArchive = $this->zipName;。错误消息指向您到底要做什么。您正在做的事情称为变量变量,尝试使用参数$ zipname的值作为属性名称。

编辑:

再次查看您的代码表明您错过了一些观点。您无需将任何参数传递给CreateZip - 您甚至不使用它们。你已经预先设置了所有必要的变量(顺便说一下,这是一个不好的做法,进入你的类并修改它的成员变量)。因此,您可以删除方法参数,或者在调用CreateZip之前删除设置值的行,并使用方法中的参数而不是类成员。

编辑2:

您在方法的后面会遇到同样的问题,例如$this->$img1

答案 1 :(得分:3)

您正在错误地引用该字段:

// create object
$zip = new ZipArchive();
$fileArchive = $this->$zipName;

应该是:

// create object
$zip = new ZipArchive();
$fileArchive = $this->zipName;

当您在属性名称前面放置一个美元符号时,PHP会在类上查找一个属性,该属性以具有该名称的变量的内容命名。

因此,正如我们从您的错误消息中看到的那样,$zipName的内容是'FileName.zip' - 但是该类没有名为'FileName.zip'的属性。

希望能够解决它。

答案 2 :(得分:0)

public function CreateZip($img1, $img2, $img3, $zipName) {
   $this->$zipName;
   //code
}

上述情况并非如您所愿。您实际在那里做的是告诉您的班级要查找文本值为$zipName的成员属性。

所以像

这样的电话
CreateZip('img1.jpg', 'img2.jpg', 'img3.jpg', 'myzipfile.zip');

将使您的脚本查找$this->myzipfile.zip,这显然不存在。只需将$this->$zipName;替换为$this->zipName;即可解决您的问题。

这也适用于其他变量$this->$img1$this->$img2