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

0 php class actionscript-3

我仍然是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 />
Run Code Online (Sandbox Code Playgroud)

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

<?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.";   
?> 
Run Code Online (Sandbox Code Playgroud)

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

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

?>
Run Code Online (Sandbox Code Playgroud)

这是班级

<?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."; 
}
}
?> 
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激

Tes*_*rex 7

简单:$fileArchive = $this->$zipName;改为$fileArchive = $this->zipName;.错误消息指向您到底要做什么.你所做的是被称为变量变量,试图使用参数$ zipname的值作为属性名称.

编辑:

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

编辑2:

你在方法的后面会遇到同样的问题$this->$img1.