0

I have installed a library using composer.json https://github.com/nuovo/spreadsheet-reader

Now in vendor I'm getting a folder structure like:

enter image description here

Now I want to use excel_reader2.php class inside my controller.

and I'm using it like:

use nuovo\spreadsheet-reader\php-excel-reader\excel_reader2;
use nuovo\spreadsheet-reader\SpreadsheetReader;

but it is giving me fatal error

Parse error: syntax error, unexpected '-', expecting ',' or ';'

Is there any way to use folders containing '-' (dash) in their names?

  • 1
    That class doesn't use namespaces. Use require as stated in the library README: require('php-excel-reader/excel_reader2.php'); – sanzante Oct 6 '16 at 11:48
  • I thing using require is not a standard way in Drupal 8. I have seen a lot of contributed modules where library is installed using composer in vendor. They guys directly used it as use folder_name\example\phpfile. The problem is not even a single library I have seen having 'dashes' in the folder name. – Sugandh Khanna Oct 6 '16 at 11:57
  • 2
    You are right, require is not a standard way in Drupal 8. But your are trying to use a library that doesn't use namespaces and is not coded in Drupal8 way, thus you can't load it using use (use doesn't include the file). To use a library that is not Drupal 8 way see this question: drupal.stackexchange.com/questions/127151/… – sanzante Oct 6 '16 at 12:12
  • make sure you are not assigining variables or function calls to your class static components. As variables and functions only initialised during runtime while static components initialised during compile time. – arpitr Oct 6 '16 at 12:13
  • Is there any alternative for this library that can be used in Drupal 8 to read excel file and converting it to array? – Sugandh Khanna Oct 6 '16 at 12:18
1

If that package is doing it correctly, then it registered its classes with composer and it should just work.

If it doesn't use namespaces (which you could see by opening the file), then just write new ClassName and it should work.

That is assuming you installed the dependency correctly, it needs to be in your main vendor folder, not the one of a module or so.

  • How to register classes with composer? Can you give any link from where I get the reference. – Sugandh Khanna Oct 6 '16 at 13:16
  • That's not your job, that's the job of the package that you use. It does that in its composer.json. For you, it should just work. – Berdir Oct 6 '16 at 13:16
  • "require": { "nuovo/spreadsheet-reader": "dev-master" } This I have written in composer.json in my module. What else I need to mention in this composer.json so that classes get registered itself. Because above piece of code is not sufficient to use classes in my controller. – Sugandh Khanna Oct 6 '16 at 13:21
  • the lib doesn't use namespacing – googletorp Oct 6 '16 at 13:25
  • Ask the author of the library. It doesn't need to use namespaces as long as it provides a classmap. I've never seen that definition that it uses, so not sure how you are supposed to you it. – Berdir Oct 6 '16 at 13:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.