Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Convertir une closure en tableau

By Saif Eddin Gmati

<?php

class X {
    private $p = 1;
    public $q = 2;
}

$x = new x;
$y = (array) $x;
print_r($y);

/*
Array
(
    [Xp] => 1
    [q] => 2
)
*/

$f = function() {};
print get_class($f);  /// Closure
var_dump((array) $f);

/*
Closure
array(1) {
  [0]=>
  object(Closure)#2 (0) {
  }
}
*/
?>

TIL : (array) $obj produit array<string, mixed> (paires clé/valeur des propriétés), sauf si $obj est une Closure, auquel cas le résultat est [$obj].

See Also

PHP Features

Last updated: 18 September 2026