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