SoapClient classmap inside a php namespace - soapfault class not found - php

I've generated a set of php classes using easyWsdl2PHP and they are working ok, but when I place the generated class inside a namespace like such...
namespace myCompany\ourService
then initially when calling this code...
$params = array("classmap"=>self::$classmap,"trace" => true,"exceptions" => true);
$this->soapClient = new SoapClient($url,$params);
I get this error
Class 'myCompany\ourService\SoapClient' not found
which I fix by calling new \SoapClient($url... (notice the backslash at the start, escapes the namespace), but now basically it is complaining it cant find the 'response' object, that is my Request is placed in a 'SALE' object and passed to the soap call, the error is like so
Uncaught SoapFault exception: [Client] Class 'SALEResponse' not found
How can I use php namespaces correctly within my code, how to get Soap in php to use the name spaces correctly? I have two very similar services I need to connect to, both with 'sale' methods, and many other commonly named elements that are not compatible, so placing them inside a php namespace appears to be a good solution, as the code is generated, I could rename each Sale to Service1_Sale and Service2_Sale, but that is far from ideal. Any help?

I've found a solution :
private static $classmap = array('SALE'=>'ourCompany\ourService\SALE'
,'SALEREQUEST_V1'=>'ourCompany\ourService\SALEREQUEST_V1'
,'AUTHORIZATION_V1'=>'ourCompany\ourService\AUTHORIZATION_V1'
,'RECEIPTREQUEST_V1'=>'ourCompany\ourService\RECEIPTREQUEST_V1'
,'SALEResponse'=>'ourCompany\ourService\SALEResponse'
);
where i've added the ourCompany\ourService\ to include the namespace i'm using.

Related

Namespace conflict when using 3rd party api in class

There is something weird going on. I'm a novice with respect to php programming.
I'm trying to use FirePHP but my question is actually not related to that tool directly.
The function fb you'll see below is part of FirePHP. My entry point is Main.php. The first fb call is executed without any problems but the second call (see ExperController.php) ,which gets triggered when $ec->exper(); in Main.php is called, causes a fatal error:
Fatal error: Uncaught Error: Class 'App\Controllers\FirePHP' not found in path-to-wp-directory\wp-content\mu-plugins\typerocket\app\Controllers\ExperController.php on line 12
Why is the runtime engine looking for the class FirePHP under that namespace (App\Controllers)? And why wasn't this an issue during the first fb call? There isn't any namespace defined in the FirePHP files at all. And my last but crucial question, how can I fix this without having to touch 3rd party files? This answer is not a solution for my case since I'm not referencing / calling the mentioned class in my code!
Note: FirePHP is included via autoloader (provided by composer).
Main.php:
<?php
fb('Hello World!', FirePHP::INFO);
$req = new \TypeRocket\Http\Request();
$res = new \TypeRocket\Http\Response();
$ec = new \App\Controllers\ExperController($req, $res);
$ec->exper();
ExperController.php:
<?php
namespace App\Controllers;
use TypeRocket\Controllers\Controller;
class ExperController extends Controller
{
public function exper() {
fb('Hello World!', FirePHP::INFO);
}
}
do you use any Dependency manager like composer??
when you call it from main somehow php can see where is FirePHP class is and everything works fine but when you try to access it inside a class in another folder you must define where the class is inside your controller , like how you defined where is controller class that you extend :
use TypeRocket\Controllers\Controller;
so add FirePHP class to your controller and everything will be ok then.
use Path\To\Class\FirePHP;
also if you did all and still not working you may try dump autoload;
composer dump-autoload

Trouble with Namepaces and Class Loading

Im building a custom CMS and have setup an autoloader, and have adapted use of namespaces. For the most part things are loading properly, but in certain cases PHP reports that it cannot find the class, the class file has been included.
Once a file is included (using require), it should be instanced as well.
The parent controller is instanced, then the child controller attempts to instance a few of its own dependencies.
$this->auth = new \Modules\Auth\RT\Auth();
This will look for a file at /modules/auth/rt/auth.php, and it does and the class is instanced properly.
The namespace of Auth is:
namespace Modules\Auth\RT;
The auth class tried to load its own dependencies, a model in particular.
$this->auth_model = new Models\ModelAuth();
Here the file to be included is at /modules/auth/rt/models/modelauth.php
It is included successfully, but this is where PHP says I cannot find this class.
Fatal error: Class 'Modules\Auth\RT\ModelAuth' not found in /Users/richardtestani/Documents/ShopOpen-Master/shopopen/modules/auth/rt/auth.php on line 12
What would cause the class from not being instanced even though the file is included?
Thanks
Rich
try this:
$this->auth_model = new Modules\Auth\RT\Models\ModelAuth();
try this:
$this->auth_model = new \Modules\Auth\RT\Models\ModelAuth();
OR
$this->auth_model = new Models\ModelAuth();
when you are in this namespace \Modules\Auth\RT
There was a Missing \ , so the code trys to include the namespace twice;
FOR REAL ;)

How to properly use Universal Class Loader?

I'm trying to use Universal Class Loader functionality from Phalcon into my project, however I can't get it working.
Here is how i've implemented into my app (using registerClasses).
index.php:
//...
$loader->registerClasses(
array(
"Commons" => "library/classes/CommonsClass.php"
)
);
$loader->register();
sampleController.php:
public function doAction()
{
$cc = new Commons();
}
And when I execute the controller, it throws me this exception:
Fatal error: Class 'Commons' not found in C:\the\path\to\phalcon_app\app\controllers\SomeController.php on line 63
The Phalcon Documentation just say that you need to register a class and call it in your funcion. There is something I've missed?
Ps.: The library folder is not registered anywhere (don't know if its needed), and it is in the same path as controllers, views, etc (/app/).
You should probably check your directory structure.
given:
mah_app/app/config/loader.php
mah_app/library/classes/Commons.php
I would expect this to work:
// loader.php
$loader->registerClasses(
array(
"Commons" => __DIR__ . "/../../library/classes/Commons.php"
)
)->register();
// works with $loader->registerDirs() for sure
Also, I would also suggest using namespaces and/or matching file name with class name.
Had the same problem. I commented out the namespace declaration inside the class and it worked. Version 2.0.3.

Calling Elastica class using "_" instead of "\"

I'm using Elastica search engine for my Symfony project.
Now, I'm getting the below error :
The autoloader expected class "Elastica_Query_Bool" to be defined in
file
"/blablabla/vendor/ruflin/elastica/lib/Elastica/Query/Bool.php".
The file was found but the class was not in it, the class name or
namespace probably has a typo.
If I change new \Elastica_Query_Bool() for new \Elastica\Query\Bool() in my php file, it works fine.
But I can't understand why I'm getting an error now. Any idea ?
Because when you new Elastica_Query_Bool it's looking for a class actually called Elastica_Query_Bool. And of course the actual class is called Bool.
try:
use Elastica\Query\Bool; // At the top of your file following the namespace line.
...
$bool = new Bool();
Might want to review namespaces in the php manual.

Fatal Error RecursiveIteratorIterator not found

As the title says, when I instantiate a class I get this message :
Fatal error: Class 'Envato\RecursiveIteratorIterator' not found in C:\Users\rgr\Apache\htdocs\Roland Groza [ 3.0 ]\class\envato\envato.php on line 359
You can view the class here : Class ;
I'm instantiating from another file :
require("envato.php");
$test = new Envato\EnvatoAPIWrapper();
echo "User Vitals : ".$test->get_user_vitals("chaoscod3r")."<br>";
The class is wrapped with a namespace, so that might have something to do with it, but I wasn't sure since it's been a few years since I haven't coded PHP. Hopefully someone has an idea what is it that I'm doing wrong :)
To access non-namespaced classes like the internal classes of PHP and SPL inside of a namespace you have to use the fully qualified class name like this:
new \RecursiveIteratorIterator();
or import it explicitly at the beginning:
use \RecursiveIteratorIterator;
and then use it normally like you do.
Add a use statement at the top of your namespace...
use \RecursiveIteratorIterator;
If you don't then PHP expects RecursiveIteratorIterator to exist within your current namespace, rather than in the global namespace (indicated by the leading \)

Categories