Make WordPress Core


Ignore:
Timestamp:
07/28/2017 01:14:03 AM (9 years ago)
Author:
johnbillion
Message:

General: Move the __autoload() compat function into its own file to prevent deprecated notices being thrown by the compiler in PHP 7.2.

The __autoload() function is deprecated in PHP 7.2, which means WordPress' own __autoload() compat function for PHP 5.2 needs to be moved into a separate file to prevent the PHP 7.2 compiler from complaining.

Props ayeshrajans

See #40109

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/compat.php

    r38239 r41178  
    489489endif;
    490490
    491 // SPL can be disabled on PHP 5.2
    492 if ( ! function_exists( 'spl_autoload_register' ) ):
    493         $_wp_spl_autoloaders = array();
    494 
    495         /**
    496          * Autoloader compatibility callback.
    497          *
    498          * @since 4.6.0
    499          *
    500          * @param string $classname Class to attempt autoloading.
    501          */
    502         function __autoload( $classname ) {
    503                 global $_wp_spl_autoloaders;
    504                 foreach ( $_wp_spl_autoloaders as $autoloader ) {
    505                         if ( ! is_callable( $autoloader ) ) {
    506                                 // Avoid the extra warning if the autoloader isn't callable.
    507                                 continue;
    508                         }
    509 
    510                         call_user_func( $autoloader, $classname );
    511 
    512                         // If it has been autoloaded, stop processing.
    513                         if ( class_exists( $classname, false ) ) {
    514                                 return;
    515                         }
    516                 }
    517         }
    518 
    519         /**
    520          * Registers a function to be autoloaded.
    521          *
    522          * @since 4.6.0
    523          *
    524          * @param callable $autoload_function The function to register.
    525          * @param bool     $throw             Optional. Whether the function should throw an exception
    526          *                                    if the function isn't callable. Default true.
    527          * @param bool     $prepend           Whether the function should be prepended to the stack.
    528          *                                    Default false.
    529          */
    530         function spl_autoload_register( $autoload_function, $throw = true, $prepend = false ) {
    531                 if ( $throw && ! is_callable( $autoload_function ) ) {
    532                         // String not translated to match PHP core.
    533                         throw new Exception( 'Function not callable' );
    534                 }
    535 
    536                 global $_wp_spl_autoloaders;
    537 
    538                 // Don't allow multiple registration.
    539                 if ( in_array( $autoload_function, $_wp_spl_autoloaders ) ) {
    540                         return;
    541                 }
    542 
    543                 if ( $prepend ) {
    544                         array_unshift( $_wp_spl_autoloaders, $autoload_function );
    545                 } else {
    546                         $_wp_spl_autoloaders[] = $autoload_function;
    547                 }
    548         }
    549 
    550         /**
    551          * Unregisters an autoloader function.
    552          *
    553          * @since 4.6.0
    554          *
    555          * @param callable $function The function to unregister.
    556          * @return bool True if the function was unregistered, false if it could not be.
    557          */
    558         function spl_autoload_unregister( $function ) {
    559                 global $_wp_spl_autoloaders;
    560                 foreach ( $_wp_spl_autoloaders as &$autoloader ) {
    561                         if ( $autoloader === $function ) {
    562                                 unset( $autoloader );
    563                                 return true;
    564                         }
    565                 }
    566 
    567                 return false;
    568         }
    569 
    570         /**
    571          * Retrieves the registered autoloader functions.
    572          *
    573          * @since 4.6.0
    574          *
    575          * @return array List of autoloader functions.
    576          */
    577         function spl_autoload_functions() {
    578                 return $GLOBALS['_wp_spl_autoloaders'];
    579         }
    580 endif;
     491/**
     492 * Polyfill for the SPL autoloader. In PHP 5.2 (but not 5.3 and later), SPL can
     493 * be disabled, and PHP 7.2 raises notices if the compiler finds an __autoload()
     494 * function declaration. Function availability is checked here, and the
     495 * autoloader is included only if necessary.
     496 */
     497if ( ! function_exists( 'spl_autoload_register' ) ) {
     498        require_once ABSPATH . WPINC . '/spl-autoload-compat.php';
     499}
Note: See TracChangeset for help on using the changeset viewer.