PHP Fatal Error Call to Undefined. Stop it breaking page? - php

Please forgive me if this is a duplicate question. I have searched but cant come up with an answer.
I am getting a PHP fatal error: call to undefined in my Wordpress site.
I know why this is, I am calling a method in my template file that is defined in a plugin - if the plugin isnt installed, then the template throws the error.
Is there any way I can encapsulate this method so that if it fails, the fatal error doesnt stop the rest of the page loading?
Many thanks.

You can check if the function is defined before calling it.
<?php
if (function_exists('plugin_function')) {
plugin_function();
} else {
error_log('plugin is not activated');
die();
}
?>

Related

PHP Fatal-Error: Call to a member function append_output()

I use codeigniter in my blog and since a while I get this error
PHP Fatal error: Call to a member function append_output() on a non-object in /var/www/site/blog/system/core/Loader.php on line 862
I don't know what change caused this and why it appears. The site gets rendered and send to browser completely, from views header.php, index.php to footer.php everything is there and after that this error appears. Search with google showed another site, that has this error at the very bottom of their site...
I now supressed the error with error_reporting(0) as the whole site works fine, but that's not a solution I want to stay with.
It happens on all pages, I have one Controler (blog.php) and several methods like index(), article(), archive() in it. The methods do what they are supposed to do, but when CI finished rendering the page, the error appears, with all controler methods.
What can I do to trace where this problem appears?
https://github.com/EllisLab/CodeIgniter/blob/develop/system/core/Loader.php#L938
If the error is occurring on the value returned from get_instance, here will be your problem. Although you may have to look at the version you are using to get the right line number.
Additionally:
https://github.com/EllisLab/CodeIgniter/blob/develop/system/core/Controller.php#L75
This appears to be the singleton class that function leads to, it is returning self::$instance which is created in the constructor.
To me this means the CI_Controller singleton has not been instantiated at the time that error has occurred.
Hope that helps you debug your problem.
I had the same problem. I'd overwritten the output class ($this->output) in my controller.

Is this an uncatchable fatal error?

I'm writing an application, where I thought all the errors were being handled, including fatal ones.
But now I found one error that results in a white screen, and the error only shows up in the webserver log.
$nonExistentVar + 1; // Notice error, gets caught and pretty error is displayed
$existentVar->nonExistentMethod(); // Fatal error, gets caught and pretty error is displayed
$nonExistentVar->nonExistentMethod(); // White screen, error can be seen in nginx.error.log
Is the last error uncatchable? Or what could the problem be?
I'm using Silex, not sure if that matters.
The way I understand it, exceptions can be caught but fatal errors cannot. I am curious to know how you are 'catching' the fatal error in example #2?
Why not use a php is_a() test to see if $nonExistentVar is of the correct class before attempting to call the method? Or possibly in conjunction with method_exists() if you still don't know if a class has a given method available.
Try putting only the last line:
$nonExistentVar->nonExistentMethod();
That works for me, as Symfony\Component\Debug\ExceptionHandler sends the response immediately upon encountering the first Error:
public function handle(\Exception $exception)
{
if (class_exists('Symfony\Component\HttpFoundation\Response')) {
$this->createResponse($exception)->send();
} else {
$this->sendPhpResponse($exception);
}
}

Getting PHP Fatal error

I am getting the following error: PHP Fatal error: Call to undefined function updateRecord()
I know that means that PHP is unable to locate the function I am calling.
I double checked the spelling of the function name and the include file at the top of the page, and both are correct. I am not sure why PHP isn't recognizing the function in the included file.
Here is the code:
include_once('../lib/updatesEngine_lib.php');//include the file with the function
The function call:
updateRecord($jobid);
In updatesEngine_lib.php:
function updateRecord($jobid){//Code//}
I'm at a loss for why this isn't working. Especially since there are other functions in the updateEngine_lib.php file that are structured similarly that are working.
HELP!
Check your spelling for the third time or until you get it work.
There is no magic.
If it says "Call to undefined function" - so it is.
BTW, what if you call this function from updatesEngine_lib.php - does it work?

PHP-Yii: Using Yiis logger when a request is not completed due to php level error

This is probably a yii specific question, even though I wouldn't surprised if experienced non-Yii we developers will be also able to contribute.
I often encounter the following situation:
My application fails due to a fatal error on the php level. Something like $var->property when $var==null
I want to understand how $var came to be ==null.
I'd use logs for this, however, problem is that no logs are left when a request is ended due to php error.
edit: this only happens for fatal errors. For other php errors I have my logs back
An example:
For
$nonExistingVar->someProperty;
I do have my logs recorded, as it yields PHP Error Undefined variable: nonExistingVar
However, if I do define the variable and set it to null,
$tmp = null;
$tmp->prop;
Then I loose the logs, as it results in "Fatal error: Call to a member function hasErrors() on a non-object"
Does anybody understand why does it happen? And how can the logger be anyway used in this situations? I tried setting autoFlush=1, doesn't help
Thanks
Gidi
The below allowed me to have my logs even on fatal errors:
function yiiCorrectShutdown()
{
Yii::app()->end();
//the following line will work as well
//Yii::app()->log->processLogs(null);
}
register_shutdown_function('yiiCorrectShutdown');
I wrote an extension class that logs in real-time thus avoiding any need to modify flow paths. I posted it here on the Yii Wiki

Trying to install Auth for CI: Call to undefined method CI_Loader::setdata()

I have been trying to implement an auth system for Codeigniter. I wanted to save time, though it hasn't succeeded so far.
The system I'm trying to implement is: http://codeigniter.com/wiki/auth/
Currently I have some forms working, but the registration form generates a fatal error:
PHP Fatal error: Call to undefined method CI_Loader::setdata() in /Applications/MAMP/htdocs/CI+Login/system/application/controllers/auth.php on line 159
Anyone has an idea what that is about? Anyone has got this system running?
thx.
EDIT:
The code that generates the error is:
if ($this->config->item('auth_use_security_code'))
$this->authlib->register_init();
$data['countries'] = $this->Usermodel->getCountries();
$this->load->setdata($data);
The problem is that load does not contain a method named setdata, has it in a previous version of CI or what can I make of this?
Try this:
$this->load->vars($data);
or remove this line and use the second parameter of the $this->load->view() function.
$this->load->view($this->config->item('auth_register_view'),$data);

Categories