PHP Classes

PHP Hydrator Object: Inject values in object variables that are private

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 115 All time: 9,571 This week: 660Up
Version License PHP version Categories
hydrator 1.0MIT/X Consortium ...5.4PHP 5, Language, Testing
Description 

Author

This class can Inject values in object variables that are private.

It takes an object of a given class and an array with property values and sets the object properties to the array values using reflection.

If the class properties are private, the class will make the reflection property object accessible, so the object values can be changed.

Picture of Aleksandar Zivanovic
  Performance   Level  
Name: Aleksandar Zivanovic <contact>
Classes: 16 packages by
Country: Serbia Serbia
Age: 30
All time rank: 16206 in Serbia Serbia
Week rank: 214 Up2 in Serbia Serbia Up
Innovation award
Innovation award
Nominee: 4x

Example

<?php

require_once 'Hydrator.php';

/**
 * Example object
 */
class UserEntity
{
   
/** @var string */
   
private $firstName;

   
/** @var string */
   
private $lastName;

   
/** @var string */
   
public function getDisplayName()
    {
        return
"{$this->firstName} {$this->lastName}";
    }
}

$user = Hydrator::hydrate(new UserEntity(), [
   
'first_name' => 'John',
   
'last_name' => 'Smith',
]);

var_dump($user->getDisplayName());


  Files folder image Files (2)  
File Role Description
Accessible without login Plain text file example.php Example example script
Plain text file Hydrator.php Class class itself

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:115
This week:0
All time:9,571
This week:660Up