Wednesday, February 9, 2022

[FIXED] Unable to load class php-ds classes in PHP 7 as I get fatal error:class not found

Issue

I am new to PHP 7 development and I have started working with Composer and PHP. I need to use PHP 7 Data structure like Vector and Stack etc. For this I have created a composer.json in my root directory to require php DS. After composer install, it has created a folder called Vendor and the PHP ds folder is inside it.

I am using the below code in a robots.php file inside the root directory.

<?php
use Ds\Stack;
use Ds\Vector;

$Vector = new Vector();

$stack = new  Stack(); 

I get fatal error saying class not found. I am not aware of how the autoloading works completely. Will I be able to call these class from my php file in the root folder?


Solution

Autoloading

For libraries that specify autoload information, Composer generates a vendor/autoload.php file. You can simply include this file and start using the classes that those libraries provide without any extra work:

require __DIR__ . '/vendor/autoload.php';

use Ds\Stack;
use Ds\Vector;

$Vector = new Vector();

$stack = new  Stack(); 

I suggest you take a look at https://getcomposer.org/doc/01-basic-usage.md



Answered By - Azael

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.