PHP code example of polidog / object-to-array

1. Go to this page and download the library: Download polidog/object-to-array library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

polidog / object-to-array example snippets




use function Polidog\ObjectToArray\object_to_array;

$date = new \DateTime();
$dateArray = object_to_array($data);

var_dump($dataArray);



use Polidog\ObjectToArray\ToArrayTrait;


class User
{
    use ToArrayTrait;

    private int $id;

    private string $name;

    private \DateTimeInterface $createdAt;

    /**
     * User constructor.
     * @param int $id
     * @param string $name
     */
    public function __construct(int $id, string $name)
    {
        $this->id = $id;
        $this->name = $name;
        $this->createdAt = new \DateTime();
    }
}

$user = new User(1, 'polidog');
var_dump($user->__toArray());