PHP Objects broken, $this is undefined - php

The code:
<?php
class My_Test{
public $exists = 'yah';
public function test(){
return $this->exists;
}
}
$test = new My_Test;
echo $test->test();
produces the following error intermittently (every other page request on average) on two servers:
( ! ) Notice: Trying to get property of non-object in test.php on line 8
Call Stack
# Time Memory Function Location
1 0.0003 636392 {main}( ) ../test.php:0
2 0.0003 636840 My_Test->test( ) ../test.php:14
where line 10 is `return $this->exists;
Please note this is not an untested example, it is the full code that produces the error. I'm aware the code is valid but it doesn't work in two environments.
I'm really not sure why! One server is running PHP 5.3.3 (fedora), the other PHP 5.3.2 (ubuntu). I've tried rebooting the servers too. They don't share anything, although they are on the same network.
Anyone got any tips for debugging?

There's nothing wrong with your test code. It runs without issue on PHP 5.3.3. You've probably lost the cause of the issue when converting your "real" code into test code to post on here.

As people before stated the code looks fine and should not cause any errors.
Therefore I guess it is related to an error of the php engine itself:
How did you install PHP?
Did you compile the sourcecode yourself? Have ./configure or make logged any warnings or error messages while executing?
Have you installed PHP by use of a package manager? (I guess everything should be fine then).
How did you configure PHP?
Have you made any changes to php.ini like changing memory limit, paths, etc. ?
Have you installed any extension that might cause issues? My experience says often opcode caches (e.g. APC) can cause errors that cannot be easily explained. Also debuggers or other extensions may cause interferences.
Do you have any (additional) error messages in your HTTP-Daemon-Log?
Sometimes additional errors can be found in the error.log file in case the server is configured to do so (e.g. unexpected termination of script or similar).

The code appears to work as is, but I've never seen an object instantiated without the parens.
$test = new My_Test;
// Should be
$test = new My_Test();
You could also implement some logging:
public function test()
{
// Can edit this to use instanceof or getclass for further testing
if (!is_object($this)) {
error_log(serialize($this));
}
return $this->exists;
}

Related

What does "i360: Error in global initialization" mean in PHP?

A PHP software I wrote has been working fine for years suddenly throws this error: i360: Error in global initialization
This error is being thrown from the callback function register_shutdown_function('my_shutdown');
The callback function is just something this:
function my_shutdown ()
{
chdir(getcwd());
$e = error_get_last();
if ($e)
trigger_error($e['message'].' on '.$e['file'].' ('.$e['line'].')', E_USER_ERROR);
}
The full error message that trigger_error throws is:
i360: Error in global initialization 1 on Unknown (0).
It doesn't give much clue. Any ideas what could be causing it?
Update 1:
If I comment out the entire my_shutdown() function, the script works fine but I am still intrigue as to why this error happened just today after years of working fine.
Update 2:
Tentative info: this appears to be related to Imunify360, a security software for web servers (which my host uses that I'm not aware of or have control over). Investigation ongoing.
This issue is caused by Imunify360 because of a recent update to include a feature called "Proactive Defense":
https://www.imunify360.com/blog/meet-imunify360-with-proactive-defense-the-sophisticated-protection-against-any-kind-of-malware-all-in-one-nice-package
To fix this you need to have your host disable the extension over all PHP versions:
sed -i "s/extension=i360.so/;extension=i360.so/g" /opt/alt/php*/etc/php.ini
That should fix the problem for the time being.
This error happened just today. It disappears when I delete my cron jobs on Hawk Host cpanel.
This error was confirmed to be coming from Imunify360 (a security software for web servers). At the time of this writing, the issue was already fixed on their end. I presume an update was applied to servers using it so if you're still getting this error, contact your host.

Is PHP ArgumentCountError server specific?

I am facing very weird problem since last two days, I have two different servers one is windows(php v5.3) and second is linux(php v7.1 ngnix). I have a project(written by someone) which is already running on windows server, now I have to move this project on linux server (with no code change). My issue, There is a login function -
Function userAuthentication($username,$password,$locationId,$url,$source)
{
....
}
And It is calling as (On windows server)
userAuthentication($username, $password,'',$url) ;
I know parameters are mismatched in this case but as I tested, this code is actually giving me result and working fine on windows but that same code is giving me obvious error on Linux server-
ArgumentCountError: Too few arguments to function
Authentication::userAuthentication(), 4 passed in
It can be resolve by changing that code but I don't want (can to be many function with same error).
I already googled but not found anything. I am very curious to know Why this is not happening on windows. Is that ArgumentCountError server specific or windows server not taking it as critical as linux, or php version issue or any other config.? what I suppose to fix to resolve this issue.?
Anyone have any idea about that please help me out, Thanks
The difference isn't in the Environment (windows vs. linux) rather the PHP version.
As you can see in the migration from PHP 7.0 to 7.1 (link: http://php.net/manual/en/migration71.incompatible.php)
Throw on passing too few function arguments
Previously, a warning would be emitted for invoking user-defined
functions with too few arguments. Now, this warning has been promoted
to an Error exception. This change only applies to user-defined
functions, not internal functions. For example:
<?php
function test($param){}
test();
The above example will output something similar to:
Fatal error: Uncaught ArgumentCountError: Too few arguments to
function test(), 0 passed in %s on line %d and exactly 1 expected in
%s:%d
How to solve it?
You'll have to define all the parameters in the function. It might be "uncomfortable" but it's a better practice as a developer. Don't be lazy regarding this issue.
This has everything to do with the newer PHP versions that are (as they should) be more strict about calling. If you want this to work again make the additional parameters optional by giving them default values.
You should update to 7.x locally to prevent this sort of issues.
Like:
Function userAuthentication($username,$password,$locationId,$url,$source = '')

json_decode doesn't work in local server

I just installed my first LAMP and NetBean in Ubuntu. I imported a small website and tried to run it both locally and remotely, but the pages that use json_decode (and other functions) don't work on the local server.
For example this page:
<?php
echo "X ";
var_dump(json_decode("{}"));
echo " Y";
?>
On the managed server shows X object(stdClass)#1 (0) { } Y, but on the local server shows X only.
I ctrl+clicked on the json_decode function name, and the file json.php opened with a json_decode function with the empty body. I don't understand if that's just the definition of an interface or the real function (I'm learning PHP), but the fact that the rendering stops there, without error messages, sounds weird.
UPDATE:
I uninstalled all the packages listed at the "Starting over" section in this page, and re-installed with sudo tasksel install lamp-server.
The next day there was a massive automatic update. I don't know if there is a relation between the installation and the update, or if the update would have come anyway.
Right now I'm still at the same position: some pages are rendered correctly locally, but my website uses json on almost every page, so I can't use the local server. (I was hoping to be able to use it in the next days, when I will be visiting the in-laws, where Internet is still an unknown concept.)
UPDATE 2:
The fact mentioned earlier that json_decode() has an empty body is not a problem. I just right clicked on session_start();, and the file session.php opened with the function defined as function session_start () {}. I don't understand why the library functions contain empty bodies, but session_start() works, so json_decode() should as well.
The fact that you are not seeing the "Y" would probably indicate a fatal error occuring during your json_decode (you could use ini_set("display_errors",1); at the top of your script to be sure)
The fatal would probably be something like:
PHP Fatal error: Call to undefined function json_decode()
Do some googling on that error. This post may help, if I am right about the fatal error:
PHP Fatal error: Call to undefined function json_decode()
Try installing the json extension separately. I think it's called php-services-json on Ubuntu, so run apt-get install php-services-json
Try not to use LAMP and install apache2 manually try if that works, maybe the LAMP package is causing you trouble. That is if you're not afraid of some extra work.
You could also check your configs if they forbid anything.

PHP script throws an impossible error - on one server, not on another

I suffer a little error and don't find a cue where to begin...
[11-Oct-2012 22:01:45] PHP Fatal error: Call to a member function
getVariableIndices() on a non-object in
/var/www/web1/html/inc/ProjectCache.php on line 545
Sound quite simple, but here is the code (with line numbers)
525 $itemData = $queryItems->fetchRows();
525 foreach ($questionData as $qData) {
526 $qp = QuestionPack::createWithData($qData, $itemData);
<snip>
538 $question = $qp->createGenerator(null);
539 if (!is_object($question)) {
540 trigger_error('Could not initialize generator ...', E_USER_WARNING);
541 continue;
542 }
543
544 // Question variables
545 $vix = $question->getVariableIndices();
<snip>
597 $question->destroy();
598 }
The method createGenerator() should always return an object. I added the subsequent IF statement for debugging reasons - never knows if my thinking is completely wrong...
The destroy method sets some instance variables (references to other objects) null, so that the garbage collector won't stick in circular references.
This problem only occurs in exactly one of about 10.000 projects on a productive system (Debian, PHP 5.3.3-7+squeeze14). Using the same script and the same data from the database, I fail to replicate it on my Windows developer system (PHP 5.3.1).
I should note that hunderts of thousands of method calls are done before the script comes to this statement (about 14 sec. processing time, because a cache is initialized during this run).
My best explanation for now is that anywhere in the megabytes of PHP script I trigger a buffer overflow or mislead the garbage collector, so that the object is released although still in use. However, the error triggers always in the same project, always with the same line (and it moves with this statement, if I place other debugging code before). This sounds quite untypical for my ideas.
Restarting the webserver did (of course) not help. Dropping the previous cache file did not help, either. This is where I am stuck. Anyone else finds this weired? Anyone who has an idea where to start??
Thanks
BurninLeo
I believe I have fixed a similar odd issue in php 5.3.2 by disabling the garbage collector via adding:
zend.enable_gc = Off
to php.ini. (and of course rebooting apache or whatever application server is parsing the php)
If that doesn't work I would suggest comparing your phpinfo() from a working server to the one with an issue. Preferably comparing linux to linux or windows to windows. Maybe something in the configuration will jump out at you.
I would also raise the error level to include notices.
Finally I would use gettype and get_class on $question when it's not an object to see what it IS.
http://www.php.net/manual/en/function.gettype.php
http://www.php.net/manual/en/function.get-class.php
Possibly call debug_backtrace as well when the error occurs.
http://php.net/manual/en/function.debug-backtrace.php
Good luck!

Very occasional fatal PHP error, "couldn't execute" autoload function

I have no idea how to go about solving this very occasional funny state that PHP gets into. So any ideas whatsoever will be appreciated.
Maybe 3 or 4 times in the past year, my site will start returning 500 errors, and the logs show:
PHP Fatal error: Couldn't execute method dw__autoload in Unknown on line 0
dw__autoload is registered with spl_autoload_register. When I restart apache the problem is gone and I forget about it for several months. I am running APC, php 5.3.2-1ubuntu4.10 on ubuntu 10.04.3. I realize there is not much to go on here!
Following the source code from the point of this error, I'd risk (psychic debugging since you have no more details) to say this happens because the autoload is being called with already an exception thrown.
That said, I can't see exactly how this can happen. The current exception is cleared both before calling the autoloader and after calling each SPL autoloader function. I don't think we ever can get to a conclusion without a script that reproduces this problem. The full set of possibilities is:
The execution was already shut down
There's a current exception
The function is no longer valid (I see that happening only due to a bug in PHP, but anyway it's not likely because it almost certainly would generate a warning before)
The autoloader function requires a reference (again, this would also generate a warning before, and your autoloader would never work anyway)

Categories