1

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?

3
  • You forgot the add a starting \ before your namespace: use \Wsdl2PhpGenerator\Generator; This \ indicates that your namespace is outside of the current namespace scope Commented Jul 6, 2016 at 8:25
  • namespace Wsdl2PhpGenerator; is reserved for the upstream package you are using, you should not use it. In your code, use your own namespaces eg namespace kajacx. You will either need to use an autoloader or require the needed files yourself. To make life easier, go ahead and check out composer: getcomposer.org Commented Jul 6, 2016 at 8:27
  • @Timothy yes i have tried adding `\` before the namespace, still i get the same error. @ymas I have used composer to get these files in the first place Commented Jul 6, 2016 at 11:00

2 Answers 2

2

Just use:

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

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.