Is PHP ArgumentCountError server specific? - php

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 = '')

Related

PHP 8.0 - method_exists on non-object causes Fatal TypeError

I'm trying to understand in the Breaking Changes for PHP 7.4 to 8.0 what is causing the below line to throw a Fatal TypeError. Obviously, I know it isn't correct but need clarity on what is causing it. Perhaps it has something to do with the php.ini default value changes? I don't get any errors using PHP 7.4.
echo method_exists(false, 'pre_setup') . PHP_EOL;
I'm not sure why it isn't listed anywhere in the breaking changes (unless I'm missing something). It certainly seems like one.
The change was Bug #79462 being fixed, which aligned the behaviour of method_exists and property_exists. The decision was to go with the stricter of the two behaviours, and raise a TypeError on invalid arguments.
It's listed in the PHP 8 changelog, although I can see why that would be hard to track down.
One of the Symfony core team opened a PHP bug for it here: https://bugs.php.net/bug.php?id=79623, so you're definitely not the only person that's been affected.

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.

Symfont/Twig FatalErrorException in Twig Core.php line 1595:

Note: This is my first Symfony project, so obvious mistakes are possible.
I have just pushed my Symfony application from my local environment (where it works nicely) to a live server. Sadly, after running server:start, if I try and curl one of the local routes in the same way I tested on my local, I get the below error back:
FatalErrorException in Core.php line 1595: Parse Error: syntax error,
unexpected '.'
Note that the Core.php file is /var/www/symfony_playground/vendor/twig/twig/lib/Twig/Extension/Core.php
I didn't some searching online, and cannot see anyone else getting similar issues, so wonder whether I have a dependency issue (although I checked this).
The line in twig that is causing the issue looks like:
$ret = $object->$method(...$arguments);
Any suggestions much appreciated.
My guess is your server is not very up-to-date when it comes to PHP. Variadic functions (the ...$arguments) were introduced in PHP 5.6 so my guess is your server is running an older version than that.
Give it a debug with by echoing phpversion() or even the more detailed phpinfo().
If this is the case the only solution would be to update PHP as downgrading symfony doesn't really work.

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 Objects broken, $this is undefined

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;
}

Categories