Jump to content

Issue with 8.1 Won't load class


phives

Recommended Posts

I am using open source PDF and Excel software.  All run perfectly in previous versions of PHP. However, when moving code to 8.1 the following error is reported. In this case it happens to be with PhpSpreadsheet. But it also happens with other packages. Appears to find the file correctly but then has issues. I've been using PHP for a while, but I'm not really familiar with the inner workings.

Uncaught Error: Class "var\\www\\html\\STG\\PhpOffice\\PhpSpreadsheet\\Spreadsheet" not found

Here is the code I am using:

use \var\www\html\STG\PhpOffice\PhpSpreadsheet\Spreadsheet;
use \var\www\html\STG\PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$activeWorksheet = $spreadsheet->getActiveSheet();
$activeWorksheet->setCellValue('A1', 'Hello World !');

$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');

 

Link to comment
Share on other sites

You don't specify a filesystem path when you use a class, you specify it's namespace path.

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

As shown in the documentation example

You also need to include the autoload.php file (again, see the example) or the class will not get loaded.

 

Link to comment
Share on other sites

Where do I find the autoload file?

 

 

Here is error message

 

ubuntu@ip-172-31-91-195:~$ composer require phpoffice/phpspreadsheet

Installation failed, deleting ./composer.json.

In RequireCommand.php line 210:
                                                                                                                          
  No composer.json present in the current directory (./composer.json), this may be the cause of the following exception.  
                                                                                                                          

In InitCommand.php line 904:
                                                                                                                              
  Package phpoffice/phpspreadsheet has requirements incompatible with your PHP version, PHP extensions and Composer version:  
    - phpoffice/phpspreadsheet 1.29.0 requires ext-dom * but it is not present.                                               
    - phpoffice/phpspreadsheet 1.29.0 requires ext-simplexml * but it is not present.                                         
    - phpoffice/phpspreadsheet 1.29.0 requires ext-xml * but it is not present.                                               
    - phpoffice/phpspreadsheet 1.29.0 requires ext-xmlreader * but it is not present.                                         
    - phpoffice/phpspreadsheet 1.29.0 requires ext-xmlwriter * but it is not present.                                         
    - phpoffice/phpspreadsheet 1.29.0 requires ext-zip * but it is not present.                                               
                                                                                                                              

require [--dev] [--dry-run] [--prefer-source] [--prefer-dist] [--prefer-install PREFER-INSTALL] [--fixed] [--no-suggest] [--no-progress] [--no-update] [--no-install] [--update-no-dev] [-w|--update-with-dependencies] [-W|--update-with-all-dependencies] [--with-dependencies] [--with-all-dependencies] [--ignore-platform-req IGNORE-PLATFORM-REQ] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--apcu-autoloader-prefix APCU-AUTOLOADER-PREFIX] [--] [<packages>...]

Edited by phives
Link to comment
Share on other sites

51 minutes ago, phives said:

Where do I find the autoload file?

It is generated by composer once all your dependencies have been installed.  It appears you may not be familiar with composer, so you should probably take some time to read it's documentation and get familiar with it and how it works.

53 minutes ago, phives said:

Package phpoffice/phpspreadsheet has requirements incompatible with your PHP version, PHP extensions and Composer version:  
    - phpoffice/phpspreadsheet 1.29.0 requires ext-dom * but it is not present.                                               
    - phpoffice/phpspreadsheet 1.29.0 requires ext-simplexml * but it is not present.                                         
    - phpoffice/phpspreadsheet 1.29.0 requires ext-xml * but it is not present.                                               
    - phpoffice/phpspreadsheet 1.29.0 requires ext-xmlreader * but it is not present.                                         
    - phpoffice/phpspreadsheet 1.29.0 requires ext-xmlwriter * but it is not present.                                         
    - phpoffice/phpspreadsheet 1.29.0 requires ext-zip * but it is not present.            

These errors mean that your PHP installation is missing extensions that are required by the phpoffice/phpspreadsheet package.  Specifically, you must install the DOM Extension, SimpleXML extension, XML extension, XML Reader extension, XML Writer extension, and ZIP extension.  If you obtained PHP through your systems package manager, it will probably also have packages for these extensions that you need to install.  You'll have to do some research on your particular package manager to find out what these packages are and get them installed.

 

Link to comment
Share on other sites

What errors did you get? Assuming you ran composer install phpoffice/phpspreadsheet as per the instructions, you'll have a composer.json and composer.lock file in your main directory, and an autoload.php in the /vendor directory.

Edited by maxxd
hit enter too early
Link to comment
Share on other sites

OK - couple questions. Can you get to the site main directory (also known as the document root) on the command line, and are you working locally or on a remote server? If the answer to the first question is yes then the answer to the second matters less, but if so please come back and we'll all talk about development best practices.

So let's assume your working directory is your document root and is located at /var/www/html. First and foremost, you'll use your terminal application to navigate into that directory:

cd /var/www/html

Then, you use Composer to install the package:

composer require phpoffice/phpspreadsheet

You should now see a /var/www/html/vendor directory. Again, in this example your php files should be located in /var/www/html.

Then, in your php files, you'll add the following in order to use the package:

<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php');

use PhpOffice\PhpSpreadsheet\Spreadsheet;

$spreadsheet = new Spreadsheet();

Just to be perfectly clear, this is not the best structure for a website - you should have all your vendor files above the document root and they should be included by the files in your document root, but that's a step too far for right now and I don't want to overwhelm.

Link to comment
Share on other sites

Assuming you start with a new, empty directory, running composer require phpoffice/phpspreadsheet would give you output like this:

keith@keith-framework:~/Workspace/Demo$ composer require phpoffice/phpspreadsheet
./composer.json has been created
Running composer update phpoffice/phpspreadsheet
Loading composer repositories with package information
Updating dependencies
Lock file operations: 9 installs, 0 updates, 0 removals
  - Locking ezyang/htmlpurifier (v4.16.0)
  - Locking maennchen/zipstream-php (3.1.0)
  - Locking markbaker/complex (3.0.2)
  - Locking markbaker/matrix (3.0.1)
  - Locking phpoffice/phpspreadsheet (1.29.0)
  - Locking psr/http-client (1.0.2)
  - Locking psr/http-factory (1.0.2)
  - Locking psr/http-message (2.0)
  - Locking psr/simple-cache (3.0.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 9 installs, 0 updates, 0 removals
  - Downloading psr/simple-cache (3.0.0)
  - Downloading psr/http-message (2.0)
  - Downloading markbaker/matrix (3.0.1)
  - Downloading markbaker/complex (3.0.2)
  - Downloading maennchen/zipstream-php (3.1.0)
  - Downloading ezyang/htmlpurifier (v4.16.0)
  - Downloading phpoffice/phpspreadsheet (1.29.0)
  - Installing psr/simple-cache (3.0.0): Extracting archive
  - Installing psr/http-message (2.0): Extracting archive
  - Installing psr/http-factory (1.0.2): Extracting archive
  - Installing psr/http-client (1.0.2): Extracting archive
  - Installing markbaker/matrix (3.0.1): Extracting archive
  - Installing markbaker/complex (3.0.2): Extracting archive
  - Installing maennchen/zipstream-php (3.1.0): Extracting archive
  - Installing ezyang/htmlpurifier (v4.16.0): Extracting archive
  - Installing phpoffice/phpspreadsheet (1.29.0): Extracting archive
9 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
1 package you are using is looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found
Using version ^1.29 for phpoffice/phpspreadsheet

And you would end up with a directory structure like:

composer_folders.png.a72f90d3a3d0ad9f369402a0f79e4121.png

 

Running in your existing /var/www/html/STG folder, you should see the same output, with the same folders mixed in to your existing stuff.

If you're getting some other output, then posting the output would help determine the issue.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.