jack Bourke-Mckenna
jack Bourke-Mckenna

Reputation: 141

php composer class not found when library is installed

I'm quite new to using composer and I am not too sure what I am doing at this point. I am currently trying to use this Nmap library I found. now once I have this library installed using this commandcomposer require willdurand/nmap I created a index.php file with

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


$hosts = Nmap::create()->scan([ 'example.com' ]);

$ports = $hosts->getOpenPorts();

echo $ports;

?>

This is what my composer.json file

{
    "require": {
        "willdurand/nmap": "^0.5.0"
    }
}

When I run this I get PHP Fatal error: Uncaught Error: Class 'Nmap' not found in /var/www/html/nmap.php:5. I have Nmap installed on my Unix system. Any help on this issue would be great.

Upvotes: 3

Views: 294

Answers (1)

Jerodev
Jerodev

Reputation: 33186

When you do not define a current namespace, PHP looks for any references classes in the root namespace. However, it cannot find Nmap in the root namespace because it is defined in the ´Nmap´ namespace.

You have to either add the namespace to the class defenition:

$hosts = \Nmap\Nmap::create()->scan([ 'example.com' ]);

Or, add a using statement for this class at the top of your file: (under <?php ofcourse)

use Nmap\Nmap;

Upvotes: 1

Related Questions