0

I am trying to create an excel file by invoking a class. This is a class that I have written and inside this class, I am trying to invoke the PHPOFFICE library that is already installed on my system. My code is as follows:

class createXls {
                
    require 'vendor/autoload.php';
    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
        
    public function createXl($loginid, $report_index, $header, $data, $fname)
    {    

But I am unable to get this to work because clearly require cannot be placed outside the method. How do I go about doing this?

coderatlarge
  • 577
  • 3
  • 8
  • 23
  • 1
    _"because clearly require cannot be placed outside the method"_ That's exactly where it should go. If you're using PSR autoloading, none of your class files should ever have a `require` in them. Also note, a `use` inside a class denotes a trait, a `use` outside a class denotes a namespace alias/import. – Alex Howansky Apr 10 '21 at 03:30
  • so I have the require outside the method in the code I have pasted.. why is it throwing a syntax error [ unexpected: require after { ... ] – coderatlarge Apr 10 '21 at 03:34
  • 1
    _If you're using PSR autoloading, none of your class files should ever have a require in them._ – Alex Howansky Apr 10 '21 at 03:37

0 Answers0