2011-11-20 52 views
-1

我是一名c#程序员,我正在学习php中的数组和类。我试图创建一个包含类文档对象的数组列表。最后我想打印每个对象的属性。这是我的代码。在数组中添加类对象

类文献

class Document { 
    public $id; 
    public $filename; 
    public $filetype; 
    public $filesize; 
    public $datecreated; 
    public $datemodified; 

    public function __construct($id, $filename, $filetype, $filesize, $datecreated, $datemodified) { 
    $this->id = $id; 
    $this->filename = $filename; 
    $this->filetype = $filetype; 
    $this->filesize = $filesize; 
    $this->datecreated = $datecreated; 
    $this->datemodified = $datemodified; 
    } 
} 

因此是我的代码调用类。

$documents = glob("C:/xampp/htdocs/researchPortal/document_repository/student/{*.doc,*.docx,*.png}", GLOB_BRACE); 

$docArray = array(); 

//print each file name 
foreach($documents as $doc) 
{ 
$document = new Document(time(),basename($doc),substr($doc, -3),(filesize($doc)/1024),(filesize($doc)/1024)." KB",date("F d Y H:i:s.",filectime($doc)),date("F d Y H:i:s.",filemtime($doc))); 
array_push($docArray,$document); 
} 

foreach($docArray as $file) { 

echo $file; //**ERROR ON THIS LINE** 
     } 

回答

1

有一个错字:

array_push($docAray,$document); 

应该

array_push($docArray,$document); 
1

你拼错$docArray$docAray在第17行

0

$docAray是一个错字。 $docArray是你的数组。查看你犯错的行中缺少的r?