PHP 8.x ha introdotto cambiamenti significativi che rompono la retrocompatibilità. Molti plugin e temi scritti per PHP 7.x generano fatal error su PHP 8. Ecco come identificare e correggere questi problemi.
Errori PHP 8.x più comuni
I problemi di compatibilità più frequenti sono:
create_function()rimossa completamente- Passaggio di
nulla funzioni stringa (strlen, strpos, substr) - Named parameters che sovrascrivono argomenti
- Return type declarations mismatch
- Namespace conflicts con nuove classi interne
Step 1: Identificare l’Errore Esatto
Abilita il debug e leggi il messaggio completo:
# Errori tipici PHP 8.x:
Fatal error: Uncaught Error: Call to undefined function create_function()
Fatal error: strlen(): Passing null to parameter #1 ($string) of type string
Fatal error: Named parameter $post overwrites previous argument
TypeError: Return value must be of type string, null returned
Step 2: Cercare il Codice Obsoleto
Usa grep per trovare pattern problematici nel tuo codice:
# Cerca create_function deprecata
grep -rn "create_function" wp-content/plugins/ wp-content/themes/
# Cerca funzioni che potrebbero ricevere null
grep -rn "strlen\|strpos\|substr\|explode" wp-content/themes/your-theme/
# Cerca each() deprecata (PHP 7.2+, rimossa in PHP 8)
grep -rn "\beach\s*(" wp-content/
# Verifica compatibilità completa con PHPCompatibility
composer global require phpcompatibility/php-compatibility
phpcs --standard=PHPCompatibility --runtime-set testVersion 8.2 wp-content/themes/your-theme/
Patch: Null Coalescing Operator
L’errore più comune è passare null a funzioni stringa. PHP 8 non lo tollera più:
// PRIMA (PHP 7.x - funzionava)
$length = strlen( $variable );
$pos = strpos( $string, $needle );
// DOPO (PHP 8.x - patch con null coalescing)
$length = strlen( $variable ?? '' );
$pos = strpos( $string ?? '', $needle );
Il null coalescing operator ?? restituisce il valore di sinistra se non è null, altrimenti quello di destra.
Patch: create_function() Rimossa
create_function() era deprecata da PHP 7.2 ed è stata rimossa in PHP 8.0:
// PRIMA (deprecato - NON funziona in PHP 8)
$callback = create_function( '$a,$b', 'return $a + $b;' );
// DOPO - Arrow function (PHP 7.4+)
$callback = fn( $a, $b ) => $a + $b;
// DOPO - Closure classica (PHP 5.3+)
$callback = function( $a, $b ) {
return $a + $b;
};
Patch: each() Rimossa
each() è stata rimossa in PHP 8:
// PRIMA (non funziona in PHP 8)
while ( list( $key, $value ) = each( $array ) ) {
// ...
}
// DOPO - foreach
foreach ( $array as $key => $value ) {
// ...
}
Patch: Return Type Mismatch
PHP 8 è più strict sui return types:
// Errore: Return value must be of type string, null returned
function get_title(): string {
return $this->title; // $this->title potrebbe essere null
}
// Fix 1: Return type nullable
function get_title(): ?string {
return $this->title;
}
// Fix 2: Default value
function get_title(): string {
return $this->title ?? '';
}
Plugin/Tema di Terzi?
Se l’errore è in un plugin o tema non tuo:
- Aggiorna il plugin all’ultima versione
- Controlla se esiste una versione PHP 8 compatibile
- Cerca alternative se non è più mantenuto
- Patch temporanea: modifica il file (ma si perderà al prossimo update)
# Aggiorna tutti i plugin
wp plugin update --all
# Verifica versioni disponibili
wp plugin list --update=available
Migrazione complessa? Se hai un tema custom con molto codice legacy, contattaci per un audit di compatibilità PHP 8.