namespace and require_once amazonpay - namespaces

i am trying to use amazonpay and encounter the following problem;
when i if ($whatever) require_once("amazonpay_stuff.php") with content:
namespace AmazonPay
require_once "apay/config.php";
include 'apay/amazon-pay.phar';
and then in the same file where i have the require i try to use
$client = new Client($amazonpay_config);
this does not work and i get error message
Fatal error: Class 'Client' not found in ....
when i have all that code in one file;
namespace AmazonPay
require_once "apay/config.php";
include 'apay/amazon-pay.phar';
$client = new Client($amazonpay_config);
it works fine - how can i make it work with require_once?

Related

Creating object for Amazon MWS api class not working in cakephp3

I have added amazon MWS API files at webroot folder in cakephp3.And I tried to call that api classes inside my controller. But its not working. It showing following error
Fatal error: Class 'App\Controller\MarketplaceWebService_Client' not found
Here is my code within a function
require_once 'MarketplaceWebService/Samples/.config.inc.php';
require_once 'MarketplaceWebService/Model/SubmitFeedRequest.php';
require_once 'MarketplaceWebService/Client.php';
require_once 'MarketplaceWebService/Model/GetFeedSubmissionResultRequest.php';
$accesskey=AWS_ACCESS_KEY_ID;
$secretkey=AWS_SECRET_ACCESS_KEY;
$serviceUrl = "https://mws.amazonservices.com";
$config = array (
'ServiceURL' => $serviceUrl,
'ProxyHost' => null,
'ProxyPort' => -1,
'MaxErrorRetry' => 3,
);
$service = new MarketplaceWebService_Client($accesskey,$secretkey,$config,APPLICATION_NAME,APPLICATION_VERSION);
Please help me to fix this issue.
Thank you
new MarketplaceWebService_Client will try to locate that class in your current namespace. You presumably want new \MarketplaceWebService_Client instead (or something along those lines, depending on whether or not it's defined in a namespace in the implementation).
You probably also want to read up on namespaces, composer and autoloading, as all of your require_once calls can probably be eliminated with suitable updates to your environment.

Twig loader in constructor

I try to make my own Response class using twig
<?php
namespace app\library;
class Response
{
public function __construct($temp, $data)
{
$loader = new Twig_Loader_Filesystem('app/views');
$twig = new Twig_Environment($loader);
print $twig->render($temp, $data);
}
}
But when I try to use it
use app\library\Response;
error_reporting(E_ALL);
require_once "vendor/autoload.php";
$arr = array('name'=>'Bob', 'surname'=>'Dow', 'gender'=>'male','age'=>'25');
new Response('temp.php', $arr);
It gives me
Fatal error: Class 'app\library\Twig_Loader_Filesystem' not found in /var/www/PHP/app/library/Response.php on line 12
Where the mistake?
Please check carefully the error. It says that class 'app\library\Twig_Loader_Filesystem' doesn't exists. Your Response class is under app\library namespace so every class which you try to instantiate there will be looked inside this namespace as well. Basically it's the same as write
$loader = new app\library\Twig_Loader_Filesystem('app/views');
$twig = new app\library\Twig_Environment($loader);
Generally in this case you have to either type the full name of a class including its namespace or make the shorthand with the help of use statement like you did for instantiating Response class.
In your particular case classes Twig_Loader_Filesystem and Twig_Environment exist in a global namespace so you could either add \ in front of classes to state that these classes are located in a global namespace:
$loader = \Twig_Loader_Filesystem('app/views');
$twig = \Twig_Environment($loader);
or create a shorthand like this:
use Twig_Loader_Filesystem;
use Twig_Environment;

I can't use Autoloader from Symfony2 in Symfony 1.4 for load this namespaced classes

I want to use this php library with namespaced classes in my Symfony 1.4 project: https://github.com/donquixote/cellbrush.
I'm not quite familiar with the namespaces concept. So when i fisrt try the to use the main class of this library, according to its docs, i just did:
$table = \Donquixote\Cellbrush\Table\Table::create();
And i got this fatal error:
Fatal error: Class 'Donquixote\Cellbrush\Table\Table' not found in D:\SF_ROOT_DIR\apps\frontend\modules\home\actions\actions.class.php
So i searched for a solution, and supposedly there is one: stackoverflow sol 1, stackoverflow sol 1 eg, but when i try to implement it i still get the above error.
My case:
Directories and files of interest:
D:\SF_ROOT_DIR\lib\autoload\sfClassLoader.class.php
D:\SF_ROOT_DIR\lib\vendor\ClassLoader (contains:
https://github.com/symfony/ClassLoader/tree/2.6)
D:\SF_ROOT_DIR\lib\vendor\cellbrush-1.0 (contains:
https://github.com/donquixote/cellbrush.)
Code:
SF_ROOT_DIR/config/ProjectConfiguration.class.php
require_once dirname(__FILE__).'/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';
require_once dirname(__FILE__) . '/../lib/autoload/sfClassLoader.class.php';
use Symfony\Component\ClassLoader\UniversalClassLoader;
use Symfony\Component\ClassLoader\ApcUniversalClassLoader;
sfCoreAutoload::register();
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
$this->namespacesClassLoader();
$this->enableAllPluginsExcept('sfPropelPlugin');
}
public function namespacesClassLoader() {
if (extension_loaded('apc')) {
$loader = new ApcUniversalClassLoader('S2A');
} else {
$loader = new UniversalClassLoader();
}
$loader->registerNamespaces(array(
'Donquixote' => __DIR__ . '/../lib/vendor/cellbrush-1.0/src/Table'));
$loader->register();
}
}
actions.class.php
$table = \Donquixote\Cellbrush\Table\Table::create();
Thanks.
Use composer and its autoloading.
Execute:
composer require donquixote/cellbrush
Now the library is installed in vendor directory and autoloader is generated, you just need to include it. Add this line to the top of config/ProjectConfiguration.class.php:
require_once dirname(__FILE__).'/../vendor/autoload.php';

PHP namespace behaviour gives FATAL error with spl_autoload_register

I want to use namespace and spl_autoload_register together but failed with different error each time.
Please See complete code files on github.
Below are the files
a base file where create a class with namespace class.alpha.php
an include file where I define spl_autoload_register include.php
an example file which instantiate the class object eg.php
Now when I create object from eg.php it gives FATAL error but when I comment namespace line in class.alpha.php then it's working
Please see the code below.
alpha.class.php
<?php
//namespace Alpha; //<< comment and uncomment this to regenerate the error
class Alpha
{
// public static $baseDir_;
public $dir = __DIR__;
public static $baseDir_;
public function __construct()
{
echo __FILE__."=>".__METHOD__;
var_dump(self::$baseDir_, $this->dir);
$firstDir = !empty(self::$baseDir_) ? self::$baseDir_ : $this->dir;
}
}
include.php
<?php //namespace Alpha\config;
spl_autoload_extensions(".php");
spl_autoload_register('loadclass');
function loadclass($class)
{
try {
if (is_readable(strtolower($class).".class.php")) {
include_once strtolower($class).".class.php";
}
} catch (Exception $e) {
print "Exception:". $e;
}
}
//#link http://php.net/manual/en/function.spl-autoload-register.php
// spl_autoload_register(__NAMESPACE__.'Alpha\Alpha()' );
eg.php
<?php
require_once 'include.php';
/** below code works by commenting 1st line on alpha.class.php
if we un comment then below code gives Fatal error: Class 'Alpha' not found */
Alpha::$baseDir_ = '/opt/lampp/archive/';
$obj_ = new Alpha();
var_dump(get_included_files());
var_dump($obj_);
/** now we define namespace Alpha on alpha.class.php */
// $ns_ = new Alpha\Alpha(); // Fatal error: Class 'Alpha\Alpha' not found
// var_dump($ns_);
/** not working even with use statement */
// use Alpha;
// use Alpha;
// $fn = new Alpha\Alpha();
// var_dump($fn);
Please help me out to solve this issue.
Thanks
Your autoloader is receiving a request for a class of "Alpha\Alpha" if you uncomment the namespace in alpha.class.php and place the use Alpha\Alpha in eg. This means that the location it's expecting to find your class in would be alpha\alpha.class.php.
Unless you're on Windows, directory separators are typically forward slash (/). So there's a number of possible solutions.
**Possible Solution #1 - Leave all files in the same place **
If you want to leave everything where it is now, you'll need to remove the namespace from the class names in the autoloader. If you add these lines to the top of your autoloader, that will make it behave that way:
$classParts = explode("\\", $class);
$class = $classParts[count($classParts) - 1];
I would not recommend this solution though since it means that you can no longer provide the same class name in a different namespace.
Possible Solution #2 - Put namespaces in subdirectories
For this solution, you'd create a new directory "alpha" and move "alpha.class.php" into it. For autoloader changes, you can add the following lines to the top of your autoloader:
$class = str_replace("\\", "/", $class);
This will change the namespace separators from backslashes to file path separators with forward slash. This will work on windows as well as mac and linux.
Possible Solution #3 - Follow an established autoloading standard
There are already a number of standard PHP autoloading standards. PSR-0 (now deprecated) works, but PSR-4 would be recommended:
PSR-0: http://www.php-fig.org/psr/psr-0/
PSR-4: http://www.php-fig.org/psr/psr-4/
One big upside of following one of these standards is that there are already plenty of implementations for them and there's been a lot of thought put into how they should work and maintain compatibility with other libraries you may end up wanting to use. Composer (http://getcomposer.org) will allow you to set up and use both PSR-0 and PSR-4 style autoloaders based on a very simple configuration.
Anyway, for the TL;DR crowd, the issue is that the autoloader receives the entire namespaced path in order to know how to load the class. The fatal error was because the autoloader wasn't properly mapping from that namespaced class to a file system location, so the file containing the class was never being loaded.
Hope this helps.

Error using map in fatfree php

When putting Employee.php class in the same directory with index.php every thing is working fine
index.php:
<?php
$f3=require('lib/base.php');
$f3->config('config.ini');
$f3->map('/employee','Employee');
$f3->run();
When putting Employee.php class under a directory, app for examle and running the following index.php
<?php
$f3=require('lib/base.php');
$f3->config('config.ini');
$f3->map('/employee','app\Employee');
$f3->run();
I am getting an error
Internal Server Error
Fatal error: Cannot redeclare class Employee
• /home/zaky/Development/kinder/app/Employee.php:2
Employee.php:
<?php
class Employee{
function get() {die("get---");}
function post() {die("post---");}
function put() {die("put---");}
function delete() {die("delete---");}
}