user3597487
user3597487

Reputation: 41

Class error being thrown in PHP

I have NO clue why an error is being thrown with this code, any help please?

foreach ($config['commands'] as $commandName => $args) {
    $reflector = new ReflectionClass($commandName);

    $command = $reflector->newInstanceArgs($args);

    $bot->addCommand($command);
}

Error:

Fatal error: Uncaught exception 'ReflectionException' with message 'Class Command\Resolve does not have a constructor, so you cannot pass any constructor arguments'

Upvotes: 0

Views: 2863

Answers (1)

deceze
deceze

Reputation: 522024

I think the error message is pretty darn clear. The documentation also says basically the same thing:

[Throws a] ReflectionException if the class does not have a constructor and the args parameter contains one or more parameters.

The class you're you're trying to instantiate does not have a constructor, yet you're trying to instantiate it as if it had one and are passing it arguments. $args should not be passed, or it should be an empty array. You need to make $args fit the constructor, currently they don't match.

namespace Command;

class Resolve {

    // Look ma, NO CONSTRUCTOR!

}

$reflector = new \ReflectionClass('Command\Resolve');
$command = $reflector->newInstanceArgs(['foo', 'bar', 'baz']);
// doesn't match the non-existent constructor ^^^^^^^^

Upvotes: 2

Related Questions