PHP not finding class in required file - php

I am using this php wrapper for multichain's json rpc api: https://github.com/Kunstmaan/libphp-multichain in a php file.
The error I see in apache error log is:
PHP Fatal error: Class 'MultichainClient' not found in /var/www/html/new.php on line 5
I'm not sure how I should adjust my code and the wrapper looks:
My code:
<?php
require_once 'libphp-multichain/src/be/kunstmaan/multichain/MultichainClient.php';
require_once 'libphp-multichain/src/be/kunstmaan/multichain/MultichainHelper.php';
$client = new MultichainClient("http://107.170.46.124:port",{usr},{pwd});
print_r($client);
</code>
MultichainClient.php Code

The "MultichainClient" class is namespaced. You need to use the namespace as well as the class name when "calling" the class.
$client = new be\kunstmaan\multichain\MultichainClient();

Related

JSON-RPC PHP class not found error

I am using this php wrapper for multichain's json rpc api: https://github.com/Kunstmaan/libphp-multichain in a php file.
I'm not sure how I should adjust my code and I'm reluctant to adjust the libraries so I wanted to check this understanding:
<?php
require_once 'libphp-multichain/src/be/kunstmaan/multichain/MultichainClient.php';
require_once 'libphp-multichain/src/be/kunstmaan/multichain/MultichainHelper.php';
$client = new MultichainClient("http://107.170.46.124:port",{usr},{pwd});
print_r($client);
The error I see in apache error log is:
PHP Fatal error: Class 'MultichainClient' not found in /var/www/html/new.php on line 5
Previously I had the wrong question on here which was referring to a php error treating MultichainClient as a function
this is not a function, you're trying to declare an instance of a new class object. so add the new keyword
$client = new MultichainClient("multichainrpc","password","host","port",3);

PHP - Class not found, but is required

I am using the Abraham TwitterOAuth PHP SDK. I included the library at the top of index.php (File exists + I use <?php).
<?php
require 'inc/oauth/autoload.php';
var_dump(file_exists('inc/oauth/autoload.php')); //true
use Abraham\TwitterOAuth\TwitterOAuth;
Below, between the HTML I include a .php file in a div, like so:
<?php include('div.php'); ?>
In this div.php file I instantiate the TwitterOAuth class, which throws an error:
if (isset($_COOKIE['token']) && !empty($_COOKIE['token'])) {
$token = objectToArray(json_decode($_COOKIE['token']));
$connection = new TwitterOAuth(CONSUMER, SECRET, $token['oauth_token'], $token['oauth_token_secret']);
This throws the error Class 'TwitterOAuth' not found.
Note
This was functioning BEFORE I moved the code to div.php. I however need it to be seperate because in the future I will refresh the div using JQuery.
The class uses the Abraham\TwitterOAuth namespace - so you have to include them or use the fqn (fully qualified classname)
$connection = new \Abraham\TwitterOAuth\TwitterOAuth(CONSUMER, SECRET, $token['oauth_token'], $token['oauth_token_secret']);

Yii2 -windows 10 xampp path error

here i have a Crypt.php file . its a class have two function, and stored it in "\backend\components" folder. i call this file(class) in my controller using this code
$security = new \backend\components\Crypt();
at run time i am getting this error:
"Unknown Class – yii\base\UnknownClassException
Unable to find 'backend\components\Crypt' in file: E:\xampp\htdocs\pope-Admin/backend/components/Crypt.php. Namespace missing?"
in this path half of them have slash(/) and half of the part have back slash() how to solve it?
In your Crypt class file, include namespace declaration like so:
<?php
namespace backend\components;
class Crypt {
...
}
?>
Use the include(_once) or require(_once) keywords to include the Crypt.php file, then just use new Crypt(). You cant define an instance of a class like that, you have to include the filw containg the class code and only then you can use the new keyword.

Unable to load namespaced class in PHP 5.3.27

I have an issue where I seem to be unable to load a namespace that I have created. I have read a bit about namespaces on SO, but I can't see what I am missing in this case.
This is my calling code:
<?php
use \CRMPicco\User\AlertReminder;
$alert_reminder = new AlertReminder($userObj);
?>
This is my namespaced code:
<?php
namespace CRMPicco\User;
class AlertReminder
{
}
This is my error:
Fatal error: Class 'CRMPicco\User\AlertReminder' not found in
/var/www/vhosts/dev/web/login.inc.php on line 324
Line 324 is the new AlertReminder line.
This was due to some classname cleansing in my custom autoloader, which prevented the class from loading.
I refactored the class into another package and it now works as expected.

PHP: resolving types in different files/namespaces

I am trying to write a PHP client for a Thrift server written in C. I am following the tutorial given at http://chanian.com/2010/05/13/thrift-tutorial-a-php-client/.
I am very new to PHP and, I suspect, am missing some of the language fundamentals.
The code in question is:
<?php
// Setup the path to the thrift library folder
$GLOBALS['THRIFT_ROOT'] = 'Thrift';
// Load up all the thrift stuff
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
// Load the package that we autogenerated for this tutorial
require_once $GLOBALS['THRIFT_ROOT'].'/packages/encabulationgame/EncabulationGame.php';
// Several things might go wrong
try {
// Create a thrift connection (Boiler plate)
$socket = new TSocket('localhost', '65123'); <--error is here
I am getting an error "Class 'TSocket' not found in /var/www/foo/scores.php on line 22"
TSocket is defined in Thrift/transport/TSocket.php. The Thrift/transport/TSocket.php file (with comments removed) reads:
<?php
namespace Thrift\Transport;
use Thrift\Transport\TTransport;
use Thrift\Exception\TException;
use Thrift\Exception\TTransportException;
use Thrift\Factory\TStringFuncFactory;
/**
* Sockets implementation of the TTransport interface.
*
* #package thrift.transport
*/
class TSocket extends TTransport {
If I change my code to:
$socket = new Thrift\Transport\TSocket('localhost', '65123');
Then my code (well, this line, at least) no longer throws an error. I'm curious: what did the author of this tutorial do to get this code to work on his system that I'm not doing on my system?
Secondly, I attempted to solve the problem by adding the line:
use Thrift\Transport;
to my file before I create $socket, but that didn't solve the problem as I'd expected it to.
How do I convince PHP to resolve this type that's defined in another file? (There are several types in several files that I have to resolve.)
PHP does not import an entire namespace, instead you must "use" each namespaced class you wish to import. Instead of use Thrift\Transport; write
use Thrift\Transport\TSocket;
This is described in more detail in the manual under Using Namespaces: Aliasing/Importing. Is it possible that the article was written before Thrift was namespaced?

Categories