Error message: The class 'PhpOffice\PhpSpreadsheet\Spreadsheet' could not be located within the context of Codeigniter

I seem to be having an issue with using phpspreadsheet in codeigniter. Below is the snippet of the code causing the problem:

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
public function modificaAzioneFormativa(){

require 'test/vendor/autoload.php';
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue("A1", "Ciao mondo!");
$writer = new Xlsx($spreadsheet);
$writer->save('prova.xlsx');

Upon execution, I am encountering the following error message:

Fatal error: Trait 'PhpOffice\PhpSpreadsheet\Spreadsheet' not found in /Applications/MAMP/htdocs/cubo/application/controllers/Users.php on line 1400 A PHP Error was encountered

Severity: Error

Message: Trait 'PhpOffice\PhpSpreadsheet\Spreadsheet' not found

Filename: controllers/Users.php

Line Number: 1400

Backtrace:

If anyone has any insights on how to resolve this issue, your help would be greatly appreciated.

Answer №1

PHP's 'use' keyword has a variety of interpretations. It can be utilized in namespaces, traits, or associating variables with anonymous functions.

In this scenario, it seems that your code is attempting to employ a trait. However, it appears that these should actually be namespace declarations instead. If this is the case, consider moving them out of the class definition and placing them at the beginning of your file, prior to running any other code.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In PHP, extract the initial three characters from each item within a foreach loop

In the following code snippet, I am trying to extract data from a JSON array and store it in a variable: $arr = json_decode($jsondata, TRUE); $arr2 = $arr['items']['item']; echo '<ul>'; foreach ($arr2 as $val) { echo &a ...

Utilizing the higher value in a PHP query

When creating a real estate search form, I included a field for users to specify the minimum and maximum number of bedrooms they are looking for. The PHP query used to extract the search results looks like this: //check bedrooms if(!empty($_GET["room_no_m ...

Tips for optimizing efficiency on a website that receives regular live updates

I am currently developing a website that allows users to create posts directly from the main page. Additionally, there is a live update feature on the front page that displays questions posted by users in real-time. Each question displayed in the live feed ...

Infinite looping redirection

I have been following a PHP video tutorial on Lynda.com (unfortunately, it's not available online) and I encountered an error when using the code provided. The specific error message is: Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many re ...

What is the most effective way to transfer JSON data to PHP?

I'm trying to retrieve the data for 'name', 'avl_bikes', and 'coordinates' from this website into my PHP. Below is my current code: <?php @ini_set("display_errors", 1); @ini_set("error_reporting", E_ALL); $string = f ...

"What is the method for generating a composite message and delivering it as an XML response in SoapServer

I am currently in the process of setting up a SOAP web service that is supposed to return a composite message. An example of a valid instance of this message looks like the following: <dl190Response xmlns="http://pse/"> <cdhead cisprik="556 ...

Leverage the power of Laravel and ReactJS to beautifully display images

Currently, I am working on a project where I am incorporating ReactJS into a Laravel blade file. To get started, I included the React CDN and began writing code within the script tag. Everything seems to be going smoothly so far, however, I have encountere ...

Bending the rules for inventory levels in WooCommerce

Currently, I am directing users to a Contact 7 Form and adding items to the cart using a specific href link: http://example.com/checkout/?add-to-cart=1000 I am faced with a unique scenario where if there is only 1 item left of product 1000, and the user s ...

Encountering a snag in PHP/MySQL query without receiving any error message?

I am facing an issue with my code. Despite having a successful database connection, the insert statement seems to be failing without throwing any errors. Surprisingly, even if I deliberately misspell the table name, the page still continues without any e ...

The problem of JSON data being displayed instead of redirecting is persistent

Here's a unique AJAX Login solution that I developed. $(function() { $('#loginform').submit(function(){ var uname = $('#username').val(); var password = $('#password').val(); $("#loading").show(); ...

Discover the best method for transferring MySQL data between pages

Can anyone help guide me in the right direction on how to allow a user to click a link within a PHP While Loop generated MySQL table and pass that data to another page? I've searched through similar questions but haven't found a solution that fit ...

Steps to change the name of the key in a foreach iteration?

Currently, I am working on creating an XML file from a database query. The utility class that I am utilizing is giving me an error message stating Illegal character in tag name. tag: 0 I have identified that this issue stems from the array elements being ...

Tips for triggering a DOM refresh while executing numerous Ajax tasks consecutively

When sending emails to customers from a page, there is a send all button available for convenience. However, this feature seems to be malfunctioning by either failing to redraw the page or not handling all the Ajax calls effectively. The JavaScript code r ...

implementing automatic ajax requests when user scrolls

This is a piece of JavaScript code: $(window).scroll(function() { $.ajax({ type: "GET", url: "not_data.php", data: dataString, success: function my_func () { //show new name ...

Struggling with XML parsing using JQuery

Currently, I am tackling a project that involves creating an endless scroller designed to fetch information from a MySQL database that has been established. As far as my scroller component goes, everything appears to be functioning correctly. Additionally, ...

What is the most effective way to communicate to my client that attempting to conceal the browser toolbar is an ill-advised decision?

One of my clients has a friend who claims to be conducting 'security testing' and insists that the PHP Zend Framework application I developed for them should implement certain browser-side features: hide the location bar, toolbar, bookmarks, me ...

Ajax: The function assigned to the route does not get executed

Pressing a button triggers a confirmation box. If 'ok' is clicked, the div called 'EventData' should display the word 'reached'. The confirmation box appears when the button is clicked, but 'EventData' does not show ...

Encountered an issue while trying to establish a connection with the Stripe API in a Laravel project:

When trying to integrate the Stripe API into my Laravel project, I encountered an error message stating: Unexpected error communicating with Stripe. If this problem persists, let us know at <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data ...

Failure of AJAX to transmit variable to PHP script

I am relatively new to PHP and currently working on the dashboard page of a website where an administrator can view all existing admins. Each admin's row has a button that is supposed to check their privileges, such as access to article editing and cl ...

Retrieve the names of subscribers on YouTube

I've been on a wild goose chase (for approximately 30 minutes :)) trying to figure out how to retrieve a list of subscriber names from a channel. Specifically, I need it for a live stream on YouTube. The software I use for recording allows me to disp ...