3

I am using notepad++. I have installed composer and from command line I have tried to install the php-ai/php-ml using the following line of code composer require php-ai/php-ml. My cmd tells me that this is successfully installed and everything seems okay.

However, in my index.php file if I try to use any of the libraries in the package php-ai/php-ml for example: use Phpml\Dataset\CsvDataset; I get the following error:

Fatal error: Uncaught Error: Class 'Phpml\Dataset\CsvDataset' not found in C:\xampp\htdocs\test\index.php:5 Stack trace: #0 {main} thrown in C:\xampp\htdocs\test\index.php on line 5

Line 5 is the line which i request to use the library. Do i have to import these libraries or anything?

neubert
  • 15,947
  • 24
  • 120
  • 212
johnfish92
  • 157
  • 2
  • 12
  • You need to include composers autoloader. Add: `require __DIR__ . '/path/to/vendor/autoload.php'` in your index.php – M. Eriksson Apr 28 '17 at 09:27
  • @MagnusEriksson i think i may have found the issue.. i seem to have more than one composer.json file, however i have located the correct one. Where should this composer file be located in order for it to use the correct file? – johnfish92 Apr 28 '17 at 09:46
  • `composer.json` should be located under each projects folder. most commonly in the projects root folder. So if you have multiple projects/sites, each should have their own `composer.json` file that defines the dependencies for that specific project. – M. Eriksson Apr 28 '17 at 10:38
  • @MagnusEriksson all i have created so far is a simple index.php in the folder htdocs that was generated from downloading XAMPP. The composer.json file is locally on my laptop however when downloading XAMPP it has downloaded composer.json file in a PHPmyadmin folder? – johnfish92 Apr 28 '17 at 10:56
  • The composer.lock and composer.json along with the vendor file are in my C:\Users\john these files hold the correct data that i need. However, when i am trying to run the PHP file it is trying to use the 3 files stated above in the PHPmyadmin folder that XAMPP downloaded. This should explain it better, i appreciate your patience @MagnusEriksson – johnfish92 Apr 28 '17 at 11:12
  • Put the composer.json and composer.lock file in the same folder as your index.php, open a command line window, go to that folder and run: "composer install". That will create the "vendor" folder, download all dependencies and create an autoload.php in your project folder. Then simply `require __DIR__ . '/vendor/autoload.php'` in the top of your index.php – M. Eriksson Apr 28 '17 at 12:04
  • I can' thank you enough @MagnusEriksson !! – johnfish92 Apr 28 '17 at 12:44
  • I've created an answer out of the above comment, so you can mark the question as answered. – M. Eriksson Apr 28 '17 at 14:25

3 Answers3

6

Composer files are dependecies for the project, so what you should do is:

  1. Move the composer.json and composer.lock file to the same folder as your index.php.
  2. Open a command line window, go to that folder and run: "composer install". That will create the "vendor" folder, download all dependencies and create an autoload.php in your project folder.
  3. Now you can put require __DIR__ . '/vendor/autoload.php' in the top of your index.php
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
1

Have you required the vendor/autoload.php file first in your script? This is required to auto load the classes from composer.

<?php

require 'vendor/autoload.php';

use Phpml\Dataset\CsvDataset;

$csv = new CsvDataset();
fire
  • 21,383
  • 17
  • 79
  • 114
  • Yes i had tried however it throws up another error on the require line. I have had a look and it seems i have 4 composer.json files on my laptop. Where should the composer.json file be located? – johnfish92 Apr 28 '17 at 09:38
  • @johnfish92 - _"it throws up another error"_ - What error do you get? – M. Eriksson Apr 28 '17 at 10:40
  • Warning: require(vendor/autoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\test\index.php on line 3 Fatal error: require(): Failed opening required 'vendor/autoload.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\test\index.php on line --- i realise this is because when downloading the XAMPP it has created this in the PEAR folder but i don't know how to fix this as it seems to have already created these folders – johnfish92 Apr 28 '17 at 10:56
0

I think you will need to install dev requirements in project root directory with Composer

bin/phpunit

Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33