This question already has answers here:
"Fatal error: Cannot redeclare <function>"
(18 answers)
Closed 3 years ago.
<?php
function date($x) {
$contents = $_FILES['userfile']['tmp_name'];
$contents = file("$contents");
$date = $contents[$x][6].$contents[$x][7]
."-".$contents[$x][8].$contents[$x][9]
."-"."20".$contents[$x][4].$contents[$x][5];
return $date;
}
?>
Fatal error: Cannot redeclare date() in .../includes.php on line 20
I have created several functions with the same exact structure as the one above and they work fine. For some reason this function keeps returning this error. Any suggestions/solutions to this problem would be greatly appreciated!
thx,
PHP already has a date() function and you cannot overwrite existing functions in this language. Rename your function and it will work. Or wrap it in a class and it will work as well.
date is an existing built-in function in PHP. You can not redeclare existing functions.
http://www.php.net/date
Fatal error: Cannot redeclare x.php (previously declared in ...)
if (!function_exists('gule')) {
function gule() {...}
}
I googled this because I could not redeclare function, as the .php file was included multiple times.
Though unrelated, somebody might get here looking for this answer because of topic. :]
Related
This question already has answers here:
"Fatal error: Cannot redeclare <function>"
(18 answers)
Closed 4 years ago.
I have some tests for my phpproject. My tests:
class myTests extends PHPunit_Framework_Testcase{
public function testValidateInventoryNumber(){
include('includes/functions.php');
$con = connectToDatabase();
$resultTest1 = validateInventoryNumber("001234", $con);
$this->assertEquals("001234", $resultTest1['data']);
}
public function testValidateLabel(){
include('includes/functions.php');
$resultTest1 = validateLabel("Sample Label.");
$this->assertEquals("Sample Label.", $resultTest1['data']);
}
}
The functions validateInventoryNumber() and validateLabel() are declared in includes/functions.php, so I need to include this file in both tests, am I right?
If I want to run the testfile, i get the following error:
Fatal error: Cannot redeclare connectToDatabase() (previously declared in C:\xampp\htdocs\prototype\includes\functions.php:4) in C:\xampp\htdocs\prototype\includes\functions.php on line 10
I think it has something to do with the includes at the start of the tests, because if I comment out one of the tests, it works properly. Any idea how it can be fixed?
Thanks to #Karlo Kokkakwho mentioned the solution in the comments. Instead of include('includes/functions.php');, use include_once('includes/functions.php');.
This question already has an answer here:
Cannot redeclare PHPMailerAutoload()?
(1 answer)
Closed 5 years ago.
I'm getting the following error when attempting to email.
Fatal error: Cannot redeclare PHPMailerAutoload() (previously declared in C:\xampp\htdocs\ISPSystem\mail\PHPMailerAutoload.php:24) in C:\xampp\htdocs\ISPSystem\mail\PHPMailerAutoload.php on line 31
you use require_once which include your file only one time so this function can not re declare .
require_once 'yourfilepath/yourfile.php';
so file link will be something like this
require_once . 'mail/PHPMailerAutoload.php';
more about require_once
http://php.net/manual/en/function.require-once.php
or check function exit or not exit then call this function
if (!function_exists('PHPMailerAutoload')) {
function PHPMailerAutoload($classname)
{
// your function code
}
}
for more information
http://php.net/manual/en/function.function-exists.php
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 7 years ago.
I am new to working with php classes, so bear with me.
I am getting this error:
"Fatal error: Using $this when not in object context in..."
I am attempting to call a function inside the same class. I am able to call the same function from another class so I am confused.
Here is the snippet from the class:
public function listLocations() {
// get all the locations
$listLocations = self::getLocations();
echo '<div class="wrap"><div id="icon-options-general" class="icon32"><br></div><h2>Locations Class</h2></div>';
foreach ($listLocations as $data) {
echo 'Location - '.$data->location.'<br>';
}
}
public function getLocations(){
$Locations = $this->db->get_results("select location_id, location FROM {$this->soups_locations_db}");
return $Locations;
}
This is inside the Class Foo_Locations
I am calling this same function using this snippet from another class. I get the results that I am looking for without error so I am confused.
$myLocations = count(Foo_Locations::getLocations());
The error is pointing to this line in the Foo_Locations Class
$Locations = $this->db->get_results("select location_id, location FROM {$this->soups_locations_db}");
But I think its related to this line:
$listLocations = self::getLocations();
This community's help is always greatly appreciated.
Thanks in advance
The problem is that you are calling getLocations statically using self::getLocations();
$this->getLocations() allows you to use the instantiated class to call getLocations().
This question already has answers here:
"Fatal error: Cannot redeclare <function>"
(18 answers)
Closed 4 years ago.
I have a function A in file B.inc
line 2: function A() {
...
line 10: }
In the apache log:
PHP Fatal error: Cannot redeclare A() (previously declared in B.inc:2) in B on line 10
I suppose you're using require "B.inc" in multiple parts? Can you try using require_once in all those instances instead?
Seems like your B.inc is parsed twice.
I had a similar problem where a function entirely contained within a public function within a class was being reported as redeclared. I reduced the problem to
class B {
function __construct() {
function A() {
}
}
}
$b1 = new B();
$b2 = new B();
The Fatal error: Cannot redeclare A() is produced when attempting to create $b2.
The original author of the code had protected the class declaration from being redeclared with if ( !class_exists( 'B' ) ) but this does not protect the internal function A() from being redeclared if we attempt to create more than one instance of the class.
Note: This is probably not the same problem as above BUT it's very similar to some of the answers in PHP Fatal error: Cannot redeclare class
Did you already declare A() somewhere else?
Or, are you calling B.inc twice on accident?
try using: require_once("B.inc");
Sounds like you might be including B.inc more than once.
// Rather than
include("B.inc");
// Do:
require_once("B.inc");
require_once() allows you to call on a file wherever necessary, but only actually parses it if not already parsed.
make sure that you require_once ( 'B.inc' ) or `include_once ( 'B.inc' )'
These people are all right, but rather use php5, autoload, and instead of functions static methods. Object related methods are mostly better but using static methods enables you to reuse a method name in many classes.
you can call a static method like this
ClassName::myFunction();
This question already has answers here:
PHP Fatal error: Using $this when not in object context
(9 answers)
Closed 9 years ago.
I am getting a Fatal error: Using $this when not in object context in Stemmer.php on line 317.
At the moment I am using the Stemmer class which I found on the internet to change words to their stemmed version before searching the database for matches.
I have read all the related posts where people are having a similar problem. The difference is that the code causing the error definitely is within object context (the code below will show that). The other strange thing is that there are parts of the code very similar to the error before and after it which don't seem to cause any difficulties. At different times the error line has changed to some of these other lines.
Does anyone have any ideas what could be causing the problem. I am using php5.1.34 if this makes any difference.
Code which calls the Stemmer class
if (isset($search) && $search != "") {
$filtered_words = WordFilter::filter($search);
foreach($filtered_words as $word) {
if(strlen($word) <= 2) {
continue;
}
$w = Stemmer::stem($word);
$stemmed_words[] = $w;
}
}
Stemmer class:
class Stemmer
{
...
if ( strlen($word) > 2 ) {
**$word = $this->_step_1($word);**
}
...
}
Even when the error occurs in difference places within the code it always seems to be when there is code trying to call another method within the same class. Could this be a bug in php5 that I am not aware of? Any advice would be most appreciated.
Thanks
Archie
Your using $this in a static method.
Static methods don't have an instance; you have to access other static properties/methods or create an instance within the static method to work with.
E.g.
Stemmer::_step_1($word);
where declared in class as
public static function _step_1($var) { [...] }
This error ocurred, because stem is not a static class, he uses $this. Try to use this code:
$Stemmer = new Stemmer;
$Stemmer->stem($word);