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

Transtypage des propriétés privées en tableau

By Paul Shryock

<?php

class MyClass {
    private $hi = 'Hi';
    protected $hello = 'Hello';
    public $goodday = 'Goodday';
}

$thing = new MyClass();
print json_encode($thing); // {"goodday":"Goodday"}

print json_encode((array) $thing); 
// {" MyClass hi":"Hi"," * hello":"Hello","goodday":"Goodday"}

TIL que PHP expose les propriétés privées lorsqu’un objet est transtypé en tableau. 🤦.

See Also

PHP Features

Last updated: 11 October 2026