另外兩個類似的問題要求屬性不能是私有的(類繼承中的未定義屬性?)和對父構造函數的調用(從父類訪問屬性時未定義屬性),這些不是我的情況的問題。我有一個繼承的類,它是 Object 實例,但我正在調用一個由父級提供的函數,該函數訪問它自己的受保護屬性。在一個對象中,代碼實際上有效,除非我進行單獨的函數調用(請參見下面的代碼),在這種情況下我會收到此錯誤,在另一個類中,即使沒有額外的行來強制出現問題,我也會在運行時收到錯誤(sendExport()錯誤嵌入到返回的 Excel 中,該 Excel 已損壞)。我看不到我的子類之間有任何區別,所以我不知道為什么行為不同,即使其中一個可以滿足我的需要,但它可能生成錯誤的事實讓我感到緊張,因此任何有關可能的問題/修復的指示會很好。錯誤是:Undefined property: App\Helpers\Exports\StudyExport::$spreadsheet at ExcelReport.php:20Line 20 is: if($this->spreadsheet instanceof Spreadsheet){這是父類 ExcelReport.php,這sendExport()是要查看的函數,它調用getExcel(),您會注意到不需要調用構造函數:<?phpnamespace App\Helpers;use PhpOffice\PhpSpreadsheet\Spreadsheet;use PhpOffice\PhpSpreadsheet\Writer\Xlsx;use Illuminate\Support\Facades\Auth;class ExcelReport { protected $cellRow=0; protected $count=0; protected $excelSec=1/24/60/60; protected $spreadsheet=null; public function checkExcel(){ return $this->spreadsheet instanceof Spreadsheet; } public function getExcel($tab=0){ if($this->spreadsheet instanceof Spreadsheet){ if($tab>=$this->spreadsheet->getSheetCount()) $tab=0; $this->spreadsheet->setActiveSheetIndex($tab); $writer=new Xlsx($this->spreadsheet); ob_start(); $writer->save('php://output'); $this->spreadsheet->disconnectWorksheets(); unset($this->spreadsheet); return ob_get_contents(); }else{ return $this->spreadsheet; } } public function sendExport($title,$tab=0){ $filename=$this->filename($title,Auth::user()->dateFormat()).'.xlsx'; // This line is not actually needed, without it, the code works (in one child but not in // another), with it the call to getExcel() gives the error $this->getExcel($tab); // /////
1 回答

慕婉清6462132
TA貢獻1804條經驗 獲得超2個贊
問題是這一行:
unset($this->spreadsheet);
這應該是:
$this->spreadsheet = null;
這并不能解決我所有的問題,但現在給了我一致的行為。我的新 Child 類仍然給我一個損壞的 Excel 文件,但這可能是由于該子類的代碼中存在一些問題,因為它現在始終適用于所有其他類。
- 1 回答
- 0 關注
- 143 瀏覽
添加回答
舉報
0/150
提交
取消