• I am trying to use an older* WP install (4.8.17) with PHP 8.
    I get this fatal error: __autoload() no longer supported, use spl_autoload_register() istead!

    do you know if it would be difficult to repair this, without knowing PHP?
    how should I rewrite that function with spl_autoload_register instead of _autoload?
    thank you in advance!

    the problematic line with the “autoload” function is in /wp-includes/compat.php and says:

    	function __autoload( $classname ) {
    		global $_wp_spl_autoloaders;
    		foreach ( $_wp_spl_autoloaders as $autoloader ) {
    			if ( ! is_callable( $autoloader ) ) {
    				// Avoid the extra warning if the autoloader isn't callable.
    				continue;
    			}
    
    			call_user_func( $autoloader, $classname );
    
    			// If it has been autoloaded, stop processing.
    			if ( class_exists( $classname, false ) ) {
    				return;
    			}
    		}
    	}

    and here is the whole context:

    // SPL can be disabled on PHP 5.2
    if ( ! function_exists( 'spl_autoload_register' ) ):
    	$_wp_spl_autoloaders = array();
    
    	/**
    	 * Autoloader compatibility callback.
    	 *
    	 * @since 4.6.0
    	 *
    	 * @param string $classname Class to attempt autoloading.
    	 */
    	function __autoload( $classname ) {
    		global $_wp_spl_autoloaders;
    		foreach ( $_wp_spl_autoloaders as $autoloader ) {
    			if ( ! is_callable( $autoloader ) ) {
    				// Avoid the extra warning if the autoloader isn't callable.
    				continue;
    			}
    
    			call_user_func( $autoloader, $classname );
    
    			// If it has been autoloaded, stop processing.
    			if ( class_exists( $classname, false ) ) {
    				return;
    			}
    		}
    	}
    
    	/**
    	 * Registers a function to be autoloaded.
    	 *
    	 * @since 4.6.0
    	 *
    	 * @param callable $autoload_function The function to register.
    	 * @param bool     $throw             Optional. Whether the function should throw an exception
    	 *                                    if the function isn't callable. Default true.
    	 * @param bool     $prepend           Whether the function should be prepended to the stack.
    	 *                                    Default false.
    	 */
    	function spl_autoload_register( $autoload_function, $throw = true, $prepend = false ) {
    		if ( $throw && ! is_callable( $autoload_function ) ) {
    			// String not translated to match PHP core.
    			throw new Exception( 'Function not callable' );
    		}
    
    		global $_wp_spl_autoloaders;
    
    		// Don't allow multiple registration.
    		if ( in_array( $autoload_function, $_wp_spl_autoloaders ) ) {
    			return;
    		}
    
    		if ( $prepend ) {
    			array_unshift( $_wp_spl_autoloaders, $autoload_function );
    		} else {
    			$_wp_spl_autoloaders[] = $autoload_function;
    		}
    	}
    
    	/**
    	 * Unregisters an autoloader function.
    	 *
    	 * @since 4.6.0
    	 *
    	 * @param callable $function The function to unregister.
    	 * @return bool True if the function was unregistered, false if it could not be.
    	 */
    	function spl_autoload_unregister( $function ) {
    		global $_wp_spl_autoloaders;
    		foreach ( $_wp_spl_autoloaders as &$autoloader ) {
    			if ( $autoloader === $function ) {
    				unset( $autoloader );
    				return true;
    			}
    		}
    
    		return false;
    	}
    
    	/**
    	 * Retrieves the registered autoloader functions.
    	 *
    	 * @since 4.6.0
    	 *
    	 * @return array List of autoloader functions.
    	 */
    	function spl_autoload_functions() {
    		return $GLOBALS['_wp_spl_autoloaders'];
    	}
    endif;

    (*The old WP is mandatory, as the theme is no more developed).

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • do you know if it would be difficult to repair this, without knowing PHP?
    how should I rewrite that function with spl_autoload_register instead of _autoload?

    Well, obviously, you can’t write/re-write PHP functions without knowing PHP ??

    But seriously, I don’t think the approach you’re taking is a tenable one. Using an ancient and abandoned theme that requires an ancient version of WordPress with bleeding-edge PHP 8 is simply calling for trouble.

    Today it’s autoload(). What else would you have to deal with tomorrow? How long can you keep hacking WordPress core, especially “without knowing PHP”?

    But if you insist :-D, see if this hack helps you: https://github.com/dzuelke/wordpress-12factor/issues/4

Viewing 1 replies (of 1 total)
  • The topic ‘PHP8: how to replace -autoload() with spl_autoload_register?’ is closed to new replies.