Unable to load namespaced class in PHP 5.3.27 - php

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.

Related

Fatal error: Cannot declare class Error, because the name is already in use

I don't understand why my machine running PHP 7.2.9 gives this error:
Fatal error: Cannot declare class Error, because the name is already in use in controllers\error.php on line 3
I have a file named controllers/error.php which contains:
<?php
class Error {
function __construct() {
echo 'Error: 404 not found the file.';
}
}
Meanwhile, I have a file named /index.php which contains:
require "controllers/error.php";
$controller = new Error;
Even if I change from require to require_once "controllers/error.php", it still keeping reporting the same message.
Error is a built-in class in PHP 7.
As such, you cannot make a class Error {}.
Rename the class, or put it in a namespace to avoid conflict.
(Or, as a third option, you could consider using/extending the built-in class instead of making your own error handling system from scratch.)
As already stated, 'Error' is a built in class in PHP 7. You have several options:
(1) Put your class in a namespace
<?php
namespace MyApp\Errors;
class Error {
}
Then when you use it ...
$error = new \MyApp\Errors\Error;
(2) Use a different name like AppError.
<?php
class AppError {
}
Recommended:
In general, classes should be placed into files that are the exact same name
as the class itself. Class "AppError" should be in file "AppError.php"
If you are using namespaces, then the namespace should match the dir where
the file is located. If you have MyApp\Errors, you would put that into "MyApp/Errors/Error.php".
class "MyApp\Errors\Error" would be in dir MyApp/Errors/Error.php.

PrestaShop module classes not found (namespaces)

This is my PrestaShop module file structure:
-mymodule/
--src/
--mymodule.php
---Presta/
---Webhooks.php
----Controller/
-----MyPrestaController.php
mymodule.php cannot find Webhooks.php class, I've tried use in mymodule.php, but still it provides errors:
ClassNotFoundException in mymodule.php line 55:
Attempted to load class "Webhooks" from namespace "src\Presta".
Did you forget a "use" statement for another namespace?
When I try to use autoload/include/require in mymodule.php it throws fatal errors, because autoload initialize stuff(from my module vendor) that shouldn't be initialized in mymodule.php. GuzzleClient gets crazy while browsing website:
Catchable Fatal Error: Argument 3 passed to
GuzzleHttp\Client::request() must be of the type array, string given,
called in /usr/local/ampps/www/presta/modules/mymodule/vendor/guzzlehttp/guzzle/src/Client.php on line 89 and defined
I don't want to put all hook logic in mymodule.php and I have other classes that I need to implement in webhook methods. Is there any way to use other classes in main module file(mymodule.php)? Am I missing something?
You need to call your class with full path or declare a use on top of your module file. I don't know exactly what namespace Webhooks is under but something like this:
public function hookActionAuthentication($params)
{
\src\Presta\Webhooks::myStaticWebhooksMethod($params);
}
or
use src\Presta\Webhooks; // before module class declaration
public function hookActionAuthentication($params)
{
Webhooks::myStaticWebhooksMethod($params);
}

php - how to remove namespace when calling object from independent class?

I'm calling an independent class's static function like: AnimeshPlugin::get_plugin_basename();
AnimeshPlugin is defined class... I'm using namespace AniPlugin\custom;
and when i call this function within this namespace class, error occured as:
Fatal error: Uncaught Error: Class 'AniPlugin\custom\AnimeshPlugin'
not found in
C:\xampp\htdocs\wordpress\wp-content\plugins\animesh-plugin\includes\config\custom\admin.php
on line 25
I don't want to add AniPlugin\custom\ before the class name.
Any suggestions??
Try the following:
\AnimeshPlugin::get_plugin_basename();
Look into Importing Namespaces in PHP and Global Space

PHP5.5: Fatal error: Cannot redeclare class

since I upgraded from PHP 5.2 to 5.5 I get an error I don't understand by now.
Fatal error: Cannot redeclare class sessionHandler in ... on line ...
Well before updating the PHP version this error didn't raise and redeclaring a class should be an error independent from PHP version as I guess. Further I always use require_once() which should help to avoid a mistake on that.
So just to make sure it doesn't be redeclared, I added a backtrace code block before that class declaration. So hopefully, I thought it would output twice, but I get only one backtrace output at all. Therefore it gets declared only once from my point of few.
Do I miss something? Any idea how to find the "real" issue?
Class "SessionHandler" already exists in the namespace as it's a class in PHP - http://php.net/manual/en/class.sessionhandler.php
Looks like the class was included in PHP 5.4 so it explains everything.
Try to think of some other name for the class or define a namespace.
If you create a namespace, something like..
namespace App;
class sessionHandler {
....
you won't get the error anymore but you will need to use App\sessionHandler whenever you're referring to your own class.

PHP - Fatal error: Cannot redeclare class Config in /path/to/Config.php on line 44

This is a WordPress local installation that I am trying to work with. I have not written a single line of this code myself. I don't understand what this error means:
Fatal error: Cannot redeclare class Config in /Applications/XAMPP/xamppfiles/lib/php/Config.php on line 44
Line 44 reads as follows:
class Config {
My guess is that a Config class has either already been declared elsewhere, or that this file is being executed for the second time.
That usually happens when you declare a class more than once in a page -- maybe via multiple includes.
To avoid this, use require_once instead. If you use require_once PHP will check if the file has already been included, and if so, not include (require) it again.
Say, for example, you have the following code:
<?php
class foo {
# code
}
... more code ...
class foo { // trying to re-declare
#code
}
In this case, PHP will throw a fatal error similar to the one below:
Fatal error: Cannot redeclare class foo in /path/to/script.php on line 7
In this case it's very simple -- simply find the 7th line of your code and remove the class declaration from there.
Alternativey, to make sure you don't try to re-declare classes, you can use the handy class_exists() function:
if(!class_exists('foo')) {
class foo {
# code
}
}
The best approach, of course, would be to organize all the configurations in one single file called config.php and then require_once it everywhere. That way, you can be sure that it will be included only once.
As for debugging the error, you could use debug_print_backtrace().
It's possible that the theme you are using refers to a file called config.php. If so use the following steps.
Try to find the config.php file and change it's name to configuration.php.
Find the files where they use config.php in the code and change it to configuration.php.

Categories