PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 9, 2022

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

 February 09, 2022     composer-php, data-structures, namespaces, php     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing