PHP - Class not found, but is required - php

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']);

Related

PHP not finding class in required file

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();

Native php. Not works namespace use

I`m use namespace in my project. But namespace not works.
file index.php
use \Folder\Aa;
Aa::test();
$test = new Aa();
file Folder/Aa.php
namespace Folder;
class Aa
{
static function test()
{
$a = 3;
echo $a;
}
}
Write me Fatal error: Class 'Folder\Aa' not found in /home/ademidko/www/first.local/index.php
I`m changes namespace in Аа.php, write "use \Folder" and other -> but not works
PHP does not load classes dynamically by itself. In order to you to be able to use your class your code have to look like this:
require_once('Folder/Aa.php');
use \Folder\Aa;
Aa::test();
$test = new Aa();
There are many possible ways how to make this work without manually writing require or require_once. One of them is to use composer's autoloading functionality (details can be found here: https://getcomposer.org/doc/04-schema.md#psr-4).
You can also consider writing your own autoloader (more details here: http://php.net/manual/en/function.spl-autoload-register.php)

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.

How can I call a function in a php class?

This is a sample code:
sample code
I want to call it in another page:
include 'root of class file';
$r = new ImagineResizer();
I got this error:
Fatal error: Class 'ImagineResizer' not found in C:\WampDeveloper\Websites\example.com\webroot\images.php on line 13
Also call the function:
$r->resize('c:/test1.jpg', 'c:/test2.jpg');
As seen in your sample code the class is located in another namespace :
<?php
namespace Acme\MyBundle\Service;
use Symfony\Component\HttpFoundation\File\File;
use Imagine\Image\ImagineInterface;
use Imagine\Image\BoxInterface;
use Imagine\Image\Point;
use Imagine\Image\Box;
class ImagineResizer {
//-- rest of code
}
To use a class in another namespace you need to point out where the file is :
First include the class (manual or with autoloading)
Then u can create an instance in 2 ways. First way with the use-keyword
use Acme\MyBundle\Service\ImageResizer;
$object = new ImageResizer();
Or point to the class absolute :
$object = new \Acme\MyBundle\Service\ImageResizer();
Hopefully, this will help you out some:
Make sure you include the actual file - not just the folder where it lies.
Make sure that the file you're calling the class from uses the same namespace as your class file. If it doesn't, you have to call the class using the full namespace.
Profit.
The namespaces really had my patience go for a spin when I started using them, but once you're used to it it's not too hard. I would recommend using an autoloader though. It's a bit of a hassle to set up, but once it's done it helps out a bunch.
Namespaces: http://php.net/manual/en/language.namespaces.php
Autoloader: http://php.net/manual/en/function.spl-autoload-register.php

PHP redeclared class from two different files

I have the code below. When I run it I get error:
Fatal error: Cannot redeclare class Google_Account in
/var/www/vhosts/example.com/httpdocs/google-api-php-client
/src/contrib/Google_AnalyticsService.php on line 379
That's because both of the "Google_AdsenseService.php" and "Google_AnalyticsService.php" files have a class named Google_Account. The member variables and functions of Google_Account class are different in that files.
I need to get Adsense and Analytics data in the same time. So I need to use both of the services at once. I couldn't find a way to un-declare classes. How can I use both of the services together?
include_once APP.'Vendor/google-api-php-client/src/Google_Client.php';
$client1 = new Google_Client();
$client1->setApplicationName('aaa');
$client1->setDeveloperKey('1234');
$client1->setRedirectUri('http://example.com/');
include_once APP.'Vendor/google-api-php-client/src/contrib/Google_AdsenseService.php';
$client1->setClientId('2345');
$client1->setClientSecret('4444');
$service1 = new Google_AdsenseService($client1);
// some code that gets data from "$service1"
$client2 = new Google_Client();
$client2->setApplicationName('aaa');
$client2->setDeveloperKey('1234');
$client2->setRedirectUri('http://example.com/');
include_once APP.'Vendor/google-api-php-client/src/contrib/Google_AnalyticsService.php';
$client2->setClientId('4567');
$client2->setClientSecret('5555');
$service2 = new Google_AnalyticsService($client2);
// some code that gets data from "$service2"
You can add different namespaces at the top of each files in contrib directory. For example for Google_AdsenseService.php file add namespace Google\AdsenseService; at the top.
// Google_AdsenseService.php file
namespace Google\AdsenseService;
As long as the file contents are only referencing the contents from same file it'll will work. Only when you access it you access by namespace. Like this,
$service1 = new Google\AdsenseService\Google_AdsenseService($client1);
You have two options:
For PHP 5.3+, you can add a namespace at the start of the file. After this, you need to fix references to other classes from the modified class (Exception will become ::Exception, etc.)
You may rename the class in a text editor, this will be probably easier. Just open up the file in your favorite text editor, and use replace all. Change Google_Client to something else. There is a good change the lib won't use dynamic class construction and other funny stuff, so your quickly refactored code will work.

Categories