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

Sérialisation rapide

By Doeke Norg

<?php

final class ValueObject{
    
    private int $uninitialized;
    private int $initialized = 3;
    
    public function toArray(): array {
        return get_object_vars($this);
    }
}

$object = new ValueObject();

var_dump($object->toArray());

// ['initialized' => 3]

Saviez-vous que get_object_vars() ne retourne que les paramètres initialisés de vos objets ? Cela peut être très utile lors de la désérialisation d’objets.

PS : accéder à des propriétés non initialisées provoque une Fatal error. Cette astuce permet de l’éviter.

See Also

PHP Features

Last updated: 11 October 2026