Cannot redeclare class - php

PHP says "Cannot redeclare class MyClass". Of course, class is defined only once. What else can cause this error?

A class included / required more then once. always use the include_once / require_once syntax for class & function files.

This was because my class was named Directory. It is probably a library class.

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.

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

FPDF - How to fix the "Fatal error: Call to undefined method PDF::FPDF()" Using Write_html

I am using this script http://fpdf.org/en/script/script50.php and I am getting the error:
Fatal error: Call to undefined method PDF::FPDF() in MyPath/html_table.php on line 55
it is this call to FPDF that is throwing the exception :
$this->FPDF($orientation,$unit,$format);
I don't understand why, knowing that the pdf class extends FPDF and I have the fpdf.php file in the same directory as the html_table.php file, is there any way to fix this error? Thank you
Fixed it.
I actually needed to replace :
$this->FPDF($orientation,$unit,$format);
by:
$this->__construct($orientation,$unit,$format);
The original script has this error, so for anyone wanting to use the script don't forget to fix this error first. Good luck.
I can't find $this->FPDF anywhere in the script you shared. When you extend a class, the extended class is in the $this of the class you extended it with.
The constructor of the extended class will always be ran when you create a new instance of this class unless you define a constructor yourself, which you did in the PDF-class as the script you shared shows.
If you want to run the constructor of the class you extended, you should do this from within the contructor of the extending class using parent::__construct(); which tells PHP that it should at that moment run the contructor of the parent class (the extended class).
This is already the case in the script you shared:
//Call parent constructor
parent::__construct($orientation,$unit,$format);
So when you run new PDF() it will call the contructor of the PDF-class, which will call the constructor of FPDF.
When you call the constructor again as mentioned in your answer by using the $this->__construct($orientation,$unit,$format); line, this will result in the PDF-contructor to be called twice.

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 " Class not found" and "Cannot redeclare class" error

I'm programming a website with PHP using Symfony2.
I defined a class MainContent in file MainController.php
And I had to use this class in other controller file named SecurityController.php
Although both of these classes are defined in the same namespace, it gave the error:
Class 'MainContent' not found...
So I tried to define the class again in SecurityController.php but result is:
Cannot redeclare class 'MainContent...'
I don't understand, it is either declared or not.
In fact I'm a C# programmer and maybe I'm confused because of some differences between these languages.
Symfony uses so-called autoloading. Since your class is not defined in it's own file, following the rules for auto-loading, it cannot be loaded, hence the class not found. But if you define the class, then script is processed successfully, other file that defines the same class is included, and the collision (cannot redeclare class) is generated.
The possible solutions are:
Define the class in one place (best outside the controller) and use include_once / require_once whenever you need it
Make the class actually a Symfony helper class, so autoloading actually works
Whenever you are declaring the class, use
if(!class_exists('ClassName')) {
class ClassName {}
}

Categories