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');
Related
I have a registration class. The problem I'm facing is that the instantiation itself is somehow causing the functions within that class to be called.
I've tested this by adding an error_log() directly before and after the instantiation: $register = new Register(); every time I receive another error_log() which I placed inside the functions of the class that I'm instantiating.
How can I solve this?
EDIT this is what an example may look like:
testclass.php
class Test {
public function test() {
error_log("Function test() was run.'");
}
}
test.php
require_once("testclass.php");
$test = new Test();
It's because your function, test() has the same name of the class Test so PHP is using it as the constructor and calling it when you instantiate it.
class Bar {
public function Bar() {
// treated as constructor in PHP 5.3.0-5.3.2
// treated as regular method as of PHP 5.3.3
}
}
As the docs say:
Warning Old style constructors are DEPRECATED in PHP 7.0, and will be
removed in a future version. You should always use __construct() in
new code.
I am relatively new to unit testing, so maybe someone can help me out here.
Problem
The following error message appears when executing the PHP unit test in the terminal:
Fatal error: Call to undefined function Path\to\missing_function() in /path/to/file.php on line 123
Normally, I would now create a dummy object using the getMock(originalClassName) function and then predefine what should be returned for missing_function() but sadly the function is not placed in any interface/class like all other functions I tested up to now.
Anyone got an idea here? Cheerio!
Generally speaking, you would just include the file with the function and have it get executed. Then you just make sure that after your code was executed, what was supposed to happen happens. You are more concerned with the outcome of the code that you are testing rather than the process of how your code is executed.
Though, if you need to mock a function there is a way using namespaces (need PHP 5.3+). In your test file, you can place a "mock" function that is in the same namespace as your code. When the function gets called php looks in the current namespace first and will find your replacement function. In the normal running of your code, it will proceed to the global namespace to call your function.
So you code would end up like this:
Your class:
namespace Foo;
class SUT {
public function bar() {
return baz();
}
}
Your test:
namespace Foo;
function bar() {
return 'boz';
}
class SUTTest extends PHPUnit_Framework_TestCase {
public function testBar() {
$sut = new SUT();
$this->assertEquals('boz', $sut->bar());
}
}
I am getting an error in PHP:
PHP Fatal error: Call to undefined function getCookie
Code:
include('Core/dAmnPHP.php');
$tokenarray = getCookie($username, $password);
Inside of dAmnPHP.php, it includes a function called getCookie inside class dAmnPHP. When I run my script it tells me that the function is undefined.
What am I doing wrong?
It looks like you need to create a new instance of the class before you can use its functions.
Try:
$dAmn = new dAmnPHP;
$dAmn->getCookie($username, $password);
I've not used dAmn before, so I can't be sure, but I pulled my info from here:
https://github.com/DeathShadow/Contra/blob/master/core/dAmnPHP.php
How to reproduce this error:
Put this in a file called a.php:
<?php
include('b.php');
umad();
?>
Put this in a file called b.php:
<?php
class myclass{
function umad(){
print "ok";
}
}
?>
Run it:
PHP Fatal error: Call to undefined function umad() in
/home/el/a.php on line 4
What went wrong:
You can't use methods inside classes without instantiating them first. Do it like this:
<?php
include('b.php');
$mad = new myclass;
$mad->umad();
?>
Then the php interpreter can find the method:
eric#dev ~ $ php a.php
ok
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();
I have a class Logger which, among other things has a method Log.
As Log is the most common use of the Logger instance, I have wired __invoke to call Log
Another class, "Site" contains a member "Log", an instance of Logger.
Why would this work:
$Log = $this->Log;
$Log("Message");
But not this:
$this->Log("Message");
The former fails with "PHP Fatal error: Call to undefined method Site::Log()"
Is this a limitation of the callable object implementation, or am I misunderstanding something?
Unfortunately, this is (still) a limitation of PHP, but it makes sense when you think about it, as a class can contain properties and methods that share names. For example:
<?php
class Test {
public $log;
public function __construct() {
$this->log = function() {
echo 'In Test::log property';
};
}
public function log() {
echo 'In Test::log() method';
}
}
$test = new Test;
$test->log(); // In Test::log() method
call_user_func($test->log); // In Test::log property
?>
If PHP were to allow the syntax you desire, which function would be invoked? Unfortunately, that only leaves us with call_user_func[_array]() (or copying $this->log to another variable and invoking that).
However, it would be nice if the following syntax was acceptable:
<?php
{$test->log}();
?>
But alas, it is not.
Same reasons you can't do this:
$value = $this->getArray()["key"];
or even this
$value = getArray()["key"];
Because PHP syntax doesn't do short hand very well.
This may work:
${this->Log}("Message");
But perhaps it's just easier and better to use the full call? There doesn't seem to be a way to get what you want to work on the one line.
The error in your question indicates it is looking for a function defined on the class which doesn't exist. An invokable object isn't a function, and it seems it can't be treated as one in this case.
as of php7.4 the following code works for me
($this->Log)("Message");