"Fatal error: Cannot redeclare function()" occurs while testing with phpunit [duplicate] - php

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

Related

When sending an email, Cannot redeclare fatal error [duplicate]

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

I am stuck with $this error using php classes [duplicate]

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

Fatal error: Call to a member function setName() on a non-object [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
I created one module in magento Local->Vehicle->Bike
In controller i write code for model:
$model_obj = Mage::getModel('bike/honda');
$model_obj->setName( $name );
It gives error like
Fatal error: Call to a member function setName() on a non-object in /opt/lampp/htdocs/magento/app/code/local/Vehicle/Bike/controllers/IndexController.php on line 40
Your call
Mage::getModel('bike/honda')
returns null. That's why $model_obj is a non-object and you get that error.
There is no function named setName() in the model bike/honda or try to use $this->model_obj->setName( $name );

PHP Fatal error: Cannot redeclare function [duplicate]

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

PHP: Cannot redeclare function error? [duplicate]

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. :]

Categories