Griesbacher / histou

Adds templates to Grafana in combination with nagflux
GNU General Public License v2.0
35 stars 16 forks source link

__autoload replaced with spl_autoload_register for PHP 8 #47

Open patricf opened 1 year ago

patricf commented 1 year ago

I recently upgraded my server that Histou and Nagios is running on and it went from PHP 7.1 to PHP 8.1 After this I couldn't get histou to work properly so I found this in the logs:

PHP Fatal error: __autoload() is no longer supported, use spl_autoload_register() instead in /usr/local/nagios/share/histou/histou/bootstrap.php on line 17

The __autoload() function in PHP has been removed in PHP 8 and according to the log you should replace it with spl_autoload_register() instead.

After some reading in the PHP documentation I found this to fix it:

Remove this from /usr/local/nagios/share/histou/histou/bootstrap.php:

function __autoload($className) {
    $file = strtolower(str_replace('\\', DIRECTORY_SEPARATOR, $className)).'.php';
    if (file_exists($file)) {
        require_once $file;
    }
}

And replace it with this:

function my_autoloader($className) {
    $file = strtolower(str_replace('\\', DIRECTORY_SEPARATOR, $className)).'.php';
    if (file_exists($file)) {
        require_once $file;
    }
}
spl_autoload_register('my_autoloader');

I'm not really a developer otherwise I've would have felt comfortable making a PR for this. If anyone else wants to do it, go ahead.

This solved my issues and I'm hoping it will save someone else some time troubleshooting.

tfmotu commented 1 year ago

Hi, I've the same error, but the fix does not work for me. When I try to read data from grafana I've the following error: " PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function {closure}(), 4 passed in /var/www/html/histou/histou/database/jsondatabase.php on line 35 and exactly 5 expected in /var/www/html/histou/index.php:16\nStack trace:\n#0 /var/www/html/histou/histou/database/jsondatabase.php(35): {closure}()\n#1 /var/www/html/histou/histou/database/influxdb.php(32): histou\database\JSONDatabase->construct()\n#2 /var/www/html/histou/index.php(49): histou\database\Influxdb->construct()\n#3 {main}\n thrown in /var/www/html/histou/index.php on line 16, " Regards

tfmotu commented 1 year ago

Hi, as a lamentable workaround I can access to the graphs if I delete the code that expects the arguments from /var/www/html/histou/index.php: " set_error_handler( function ($errno, $errstr, $errfile, $errline, array $errcontext) { // error was suppressed with the @-operator if (0 === error_reporting()) { return false; } throw new ErrorException($errstr, 0, $errno, $errfile, $errline); } ); " Regards