kajacx
kajacx

Reputation: 12959

PHP how to import class with namespace

I have never used namespaces in PHP before and I cannot import some library classes that use them. I'm trying to use the wsdl2php library, here is the example code on how to use the library:

$generator = new \Wsdl2PhpGenerator\Generator();
$generator->generate(
  new \Wsdl2PhpGenerator\Config(array(
    'inputFile' => 'input.wsdl',
    'outputDir' => '/tmp/output'
  ))
);

However, it doesnt matter where I put my php file that executes this code, it always throws class not found error, even if I include both classes with include or require, then the library class will throw class not found error, since it wants to use yet another library class.

Here is some directory tree for reference:

[src]
    [Filter]
        DefaultFilter.php
        ...
    [Wsdl2PhpGenerator]
        [Console]
            Applicaton.php
            ...
    ...
    Config.php
    Generator.php
    my_php_file_here.php

namespace declaration in Config.php: namespace Wsdl2PhpGenerator; How use is used in Config.php: use Wsdl2PhpGenerator\ConfigInterface;

From what I have seen, I have added the following to the start of my php file:

namespace Wsdl2PhpGenerator;

use Wsdl2PhpGenerator\Generator;
use Wsdl2PhpGenerator\Config;

With no success, I still get

Fatal error: Class 'Wsdl2PhpGenerator\Generator' not found in C:\kajacx\programming\PHP\EET\test\wsdl2phpgenerator-master\src\kappa.php on line 8 ($generator = new \Wsdl2PhpGenerator\Generator();)

So, how to make this work?

Upvotes: 1

Views: 3089

Answers (2)

Julien Loizelet
Julien Loizelet

Reputation: 341

as as I just worked with the wsdl2php lib (cloned from github) and had the same issue, here is what I did to resolve it :

1) don't forget to launch composer install before all ...

2) Add require_once dirname( dirname(__FILE__) ) . '/vendor/autoload.php'; at the top of your php file.

Upvotes: 1

random425
random425

Reputation: 719

Just use:

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

and composer will resolve the imports for you. (tested with the same lib and it worked)

Upvotes: 2

Related Questions