我是一名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**
}