array-to-object/array-to-object

Convert a php associative array into an object


Keywords
array, object
License
MIT

Documentation

Array InTo Object

note: Convert a php associative array into an object and provide a getter method to get the value of the corresponding key!

Usage

See /tests/ArrayToObjectTest.php and run the test.

<?php

$arrays = [
    'name' => 'Savey',
    'age'  => 18
];

/** @var User $user */
$user = ArrayToObjectUtil::convertObject(new User(), $arrays);

$this->assertInstanceOf(ArrayToObjectInterface::class, $user);

$this->assertObjectHasAttribute('name', $user);
$this->assertObjectHasAttribute('age', $user);

$this->assertEquals('Savey', $user->getName());
$this->assertEquals('18', $user->getAge());

//null default
$this->assertNull($user->getSex());

TIPS

The array must be an associative array, and the key cannot be switched with a non-letter! Otherwise it will cause the system BUG!