In debugging some code I've mistakenly made a call to a member function on a non-object, giving the expected Call to a member function get() on a non-object in /path/to/file.php on line X. I'm accustomed to this, while PHP isn't my favorite language I've been working with it for years. What's confusing me is that when I remove the offending code (I'm using version control and resetting to what should be a working state prior to the bad call), I still get the error. Is it possible for this error to have corrupted an in-memory (I'm using APC object caching). I feel like the answer might be "yes, maybe", however I honestly don't think that explains the problem and am wondering if anyone else has encountered something similar? Is there a method to somehow reset this PHP class object?
Related
I'm migrating a PHP 5.2.x application to a new 5.2.x server. The old server actually started as a PHP 4.0 server many years ago and was upgrade to PHP 5.2 over time. One of our modules has a function that gets redeclared if this module is used more than once. We can easily fix this, but we're perplexed at how it ever could have worked.
On the new server it will fail with an expected:
Fatal error: Cannot redeclare function
The problem is that on the old server it was always re-declaring the function! Is there a PHP setting or special usage being used here that makes it work on one server but not another?
Thank you!
Edit Still trying to pour through how this is possible. The site FATAL errors but has execution after that point of error.
Redeclaring functions is consider a error.
Maybe you guys can use "rename function".
http://es.php.net/manual/en/function.rename-function.php
if(function_exist("foo")){
rename_function('foo', 'old_foo' );
function foo(){
/*...*/
}
}
Another idea is to rewrite code to do this
$foo = function(){ /* something */.... };
So the next time you want to redefine $foo(), you do
$foo = function(){ /* something else */.... };
I am unable to reproduce your description, PHP 4 does not allow you to redeclare functions:
echo PHP_VERSION;
function foo() {}
function foo() {}
Demo/Output:
4.4.9
Fatal error: Cannot redeclare foo() (previously declared in /homepages/26/d94605010/htdocs/lz/writecodeonline.com/php4/index.php(138) : eval()'d code:2) in /homepages/26/d94605010/htdocs/lz/writecodeonline.com/php4/index.php(138) : eval()'d code on line 3
You must mix things here, so better find out more about facts when debugging, not guessing (yes I know can be hard sometimes, but facts help when debugging, aim for them).
And if it's a fatal error, your script ends. You can add a shutdown callback function to further debug your issue, see a related question:
PHP : Custom error handler - handling parse & fatal errors
Is there a way to catch Call to a member function foo() on a non-object in PHP? It does not sound that serious (as far as fatal errors go), but the shutdown function does not seem to be called at all (PHP 5.3, Debian).
Update:
How to prevent such errors is really not the point. Sure, one should check for null whenever that is an expected possibility, but littering every single object member function reference with error checking code would result in bloated and unreadable code. Hunting down the occassional error based on the logs is fine - the problem is that logs are not very useful for fatal errors. Using a shutdown function would solve that nicely, but I can't get it to work with this specific type of error; which seems strange to me, because it is not an error which would leave the PHP interpreter in a particularly messy state.
I hope this does not sound silly - but you should make sure you know what you are working with. Use instanceof or is_object where you need - or fix the source of the problem - why is that variable not an object in the first place?
I suggest to just ensure, that it is an object. Using methods/functions you can use type hints
public function x (myClass $object) {
$object->foo();
}
else you may use is_object(). At the end such a message sounds like there is a bug within you application, that should be fixed before release, or -- if such a situation can occur by design -- verify the type (is_object() (see above) or !is_null($obj) or something like that) before trying to call something, that does not exist.
I am using a Joomla component com_fabrik but though its install successfully. As I try to create form I am getting following error.
Fatal error: Call to a member function setId() on a non-object in C:\xampp\htdocs\sankalpJoomla\administrator\components\com_fabrik\models\form.php on line 108
When I searched for code in file I got.
$feFormModel->setId($this->getState('form.id'));
I searched that function is deprecated. is their any other option to sort out problem?
While it may be that the function is deprecated, the error you're getting isn't related to that (at least not directly). What is happening is $feFormModel isn't an actual object, and because of that it has no memeber functions. So when setId() is being called from it, this error is being thrown.
The likely cause is some sort of incompatibility between this component and the version of Joomla you're using.
Does anyone have a clue about this? PHP 5.2.13. Results not wholly consistent i.e. could get a good result with a page at one time, then get an error at another.
The error is fatal - class does not have method.
But the following are true:
The class is defined in only one place and has the relevant method in the code.
At the point where failure occurs: reflection shows that the method exists.
At the point where failure occurs: method_exists says the method does not exist.
Previous calls (they're all static - not my choice) earlier in the code worked.
May be it's related: http://bugs.php.net/bug.php?id=51425
But I think here we have some cache-related problem. Do you have some cache enabled? Like APC or any other accelerators?
Be sure that the file containing the method is included. If the method is in a class, make sure the class instance is created and the method is called through the class.
Maybe you are missing the class instance?
First of all, this question is purely theoretical. Fact is, whether it's possible or not, it would be terribly bad practice. Having said that, here's my question:
PHP offers the possibility to define custom error handlers via the set_error_handler function. By parsing the error message, it's possible to find out what triggered the error.
I'm interested mostly in 'Call to undefined function' errors. I know its possible to parse the error message to uncover the called function, and this got me thinking.
Would it be possible for the error handler, in case of an Undefined Function, to attempt to include a file (say functions.html.php for all functions starting with a html_ prefix), and then attempt to re-execute the function, plus arguments, that initially triggered the error? And, most importantly, return the function's value in case of success?
In brief without using exception handling you won't be able to recover from the error in the way you described.
There is a way to handle this specifically for undefined functions, however that is to say undefined member functions from an object. This is the __call() method. Basically if you call an undefined method from an object __call() then takes that function call and does whatever you put in the method body see http://php.net/manual/en/language.oop5.overloading.phplink text
It's not really possible to restart the execution where the error occured.
However: there is a system to loading classes on demand, using the __autoload function.
From the manual:-
You may define an __autoload function which is automatically called in case you are trying to use a class/interface which hasn't been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.
There is more in the PHP manual here: http://php.net/manual/en/language.oop5.autoload.php