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();
Related
This question already has answers here:
Calling closure assigned to object property directly
(12 answers)
Closed 8 years ago.
Is it possible to store a function in PHP object's properties like this:
class testing {
public $testvars;
function __construct(){
$this->testvars = function(){
return "Test String";
};
}
}
If it's possible, how do you call it?
I have been trying to call it like this:
$main = new testing();
$main->testvars();
But it throws an error:
Fatal error: Call to undefined method testing::testvars()
Try to call like
$this->testvars();
Considering that you are calling this function in the same class.And if you are calling this in another class you need to add this _call() function
public function __call($method, $args) {
if(isset($this->$method) && is_callable($this->$method)) {
return call_user_func_array(
$this->$method,
$args
);
}
}
to your new class and you can call it as
$main->testvars();
The real problem is: yes, PHP syntax has lack of support fur such situation. Neither {..} nor (..) may help you. The only way to access the property (without __call() magic) is:
class testing {
public $testvars;
function __construct(){
$this->testvars = function(){
return "Test String";
};
}
}
$obj = new testing();
echo call_user_func_array($obj->testvars, []);
So to pass your callback into call_user_func_array(). Note big difference with passing of [$obj, 'testvars'] (which won't work here, obviously) - since your property contains a callback, but not class contains such method. You may also use call_user_func() of course.
As for syntax support, there is an RFC which is proposed by Nikita Popov and which will allow you to resolve the issue with syntax only - so no additional function calls would be needed (Fortunately, that RFC was accepted and has real chances to be implemented in newer PHP versions).
You have to call your closure using call_user_func() or call_user_func_array().
So you can do the following:
$testing = new testing();
echo call_user_func($testing->testvars);
To call your closure outside of the class itself.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
where we use object operator “->” in php
In PHP 5, what are the advantages of typing $class::method() instead of $class->method()?
As in any performance or functional differences. Or is this just a way of forcing code non-PHP4 friendly because of the complete rewrite?
In PHP5, the two aren't interchangeable.
Static method calls will perform faster than non-static calls (over many iterations) but then the method is called in the static context and there is no object available to the called method.
The only reason PHP lets you call a non-static method using the static notation was for backwards compatibility in PHP 4 (because PHP 4 didn't have the static modifier for functions, or public/protected/private). If you do call a non-static method statically, you get a warning about "Strict Standards" output, and eventually this may fail with a fatal error.
So the answer really is to call the method the way it was supposed to be called. If it is a static method in PHP 5, then call it statically Class::method(), if it is a public method, then call it using the object $class->method().
Consider this code (run in PHP 5):
class Foo {
protected $bar = 'bar';
function f() {
echo $this->bar;
}
}
echo Foo::f(); // Fatal error: Using $this when not in object context
$class::method() calls a static method of the class whereas $class->method() calls a public standard method of the class.
In PHP 4, if you use a class before it's defined you get this error:
Fatal error: Undefined class name
'foo' in...
My code is:
function do_stuff(){
if(foo::what()) ... // this code is before the php file with the foo class is included
}
class foo{
function what(){
...
}
}
do_stuff();
is there any workaround for this (besides telling the people who use your script to update to php5) ?
You could instead use:
call_user_func(array('foo', 'what'));
which would cause the class/method to be checked at runtime rather than compile time.
Define your classes in a file which is require_once()d at the start of your script.
if php4, you can test the existence of a class with class_exists. So to be compatible with php5, you can write this type of code :
<?php
function __autoload($classname) {
include("classes/$classname.class.php");
}
if (!class_exists('foo')) __autoload('foo');
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);
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. :]