PHP Classes

Serialize: Automatic serialization of objects with getters

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (10)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 182 All time: 8,674 This week: 121Up
Version License PHP version Categories
serialiazable 1.0.4The PHP License5PHP 5, Language
Description 

Author

This class can perform automatic serialization of objects with getters.

It takes an object of a class and uses Reflection to extract the list of variables for which there is an explicit getter function.

The class calls all the object getter functions and returns a serialization of the object into a string in the JSON format.

Getter functions that return objects will be make this class also serialized the referenced objects recursively.

Innovation Award
PHP Programming Innovation award nominee
November 2016
Number 10


Prize: One downloadable copy of Komodo IDE
When you want save the information of an object you can use the serialize function or even the json_encode function.

However, these functions may generate serialized strings that contain much more information than you actually need to save.

This class provides a more efficient solution for saving only the relevant information of an object. It uses reflection to determine the name of the getter functions.

This way the class can retrieve only the values of the relevant class variables to save. It returns an object representation with their values in the JSON format.

Furthermore, getter objects that return other objects can be also serialized recursively.

Manuel Lemos
Picture of Theophilus Omoregbee
  Performance   Level  
Name: Theophilus Omoregbee <contact>
Classes: 1 package by
Country: Nigeria Nigeria
Age: 29
All time rank: 419138 in Nigeria Nigeria
Week rank: 312 Up7 in Nigeria Nigeria Up
Innovation award
Innovation award
Nominee: 1x

Example

<?php
/**
 * Created by PhpStorm.
 * User: Theophilus Omoregbee <theo4u@ymail.com>
 * Date: 10/25/16
 * Time: 12:10 AM
 */

include "Serialize.php";

/**
 * this handles the parents of the user
 */
class ParentTest
{
    protected
$email;
    protected
$password;

   
/**
     * @return mixed
     */
   
public function getEmail()
    {
        return
$this->email;
    }

   
/**
     * @param mixed $email
     */
   
public function setEmail($email)
    {
       
$this->email = $email;
    }

   
/**
     * @return mixed
     */
   
public function getPassword()
    {
        return
$this->password;
    }

   
/**
     * @param mixed $password
     */
   
public function setPassword($password)
    {
       
$this->password = md5($password);
    }
}

class
State
{
    private
$name;
    private
$code;

    function
__construct($name, $code)
    {
       
$this->name = $name;
       
$this->code = $code;
    }

   
/**
     * @return mixed
     */
   
public function getName()
    {
        return
$this->name;
    }

   
/**
     * @param mixed $name
     */
   
public function setName($name)
    {
       
$this->name = $name;
    }

   
/**
     * @return mixed
     */
   
public function getCode()
    {
        return
$this->code;
    }

   
/**
     * @param mixed $code
     */
   
public function setCode($code)
    {
       
$this->code = $code;
    }
}

class
Country
{
    private
$name;
    private
$code;
    private
$states = array();

    function
__construct($name, $code)
    {
       
$this->name = $name;
       
$this->code = $code;
    }

   
/**
     * @return mixed
     */
   
public function getName()
    {
        return
$this->name;
    }

   
/**
     * @param mixed $name
     */
   
public function setName($name)
    {
       
$this->name = $name;
    }

   
/**
     * @return mixed
     */
   
public function getCode()
    {
        return
$this->code;
    }

   
/**
     * @param mixed $code
     */
   
public function setCode($code)
    {
       
$this->code = $code;
    }

   
/**
     * @return State[]
     */
   
public function getStates()
    {
        return
$this->states;
    }

   
/**
     * @param State[] $states
     */
   
public function setStates($states)
    {
       
$this->states = $states;
    }

   
/**
     * this is used to add state to our array if states
     * @param State $state
     */
   
public function addState(State $state)
    {
       
$this->states[] = $state;
    }

}

/**
 * Create a dummy user
 */
class User extends ParentTest
{
    private
$firstname;
    private
$lastname;
    private
$states = array();
    private
$country;
    private
$dateCreated;
    private
$roles = array();


    function
__construct()
    {
       
$this->dateCreated = time();
    }

   
/**
     * @return mixed
     */
   
public function getFirstname()
    {
        return
$this->firstname;
    }

   
/**
     * @param mixed $firstname
     */
   
public function setFirstname($firstname)
    {
       
$this->firstname = $firstname;
    }

   
/**
     * @return mixed
     */
   
public function getLastname()
    {
        return
$this->lastname;
    }

   
/**
     * @param mixed $lastname
     */
   
public function setLastname($lastname)
    {
       
$this->lastname = $lastname;
    }

   
/**
     * @return mixed
     */
   
public function getStates()
    {
        return
$this->states;
    }

   
/**
     * @param mixed $states
     */
   
public function setStates($states)
    {
       
$this->states = $states;
    }


   
/**
     * @return Country
     */
   
public function getCountry()
    {
        return
$this->country;
    }

   
/**
     * @param mixed $country
     */
   
public function setCountry($country)
    {
       
$this->country = $country;
    }

   
/**
     * @return int
     */
   
public function getDateCreated()
    {
        return
$this->dateCreated;
    }

   
/**
     * @param int $dateCreated
     */
   
public function setDateCreated($dateCreated)
    {
       
$this->dateCreated = $dateCreated;
    }

   
/**
     * @return array
     */
   
public function getRoles()
    {
        return
$this->roles;
    }

   
/**
     * @param array $roles
     */
   
public function setRoles($roles)
    {
       
$this->roles = $roles;
    }

}

//done creating dummy objects
$user = new User();
$user->setFirstname("Theophilus");
$user->setLastname("Omoregbee");

$user->setStates(array(new State("Edo state", "ED"), new State("Lagos State", "LG")));
$user->setCountry(new Country("Nigeria", "NG"));

$user->getCountry()->setStates($user->getStates());

$user->setRoles(array("ADMIN", "DB MANAGER"));

$user->setEmail("theo4u@ymail.com");
$user->setPassword("1111");

//lets serialize our object now
header('Content-type: application/json');
echo
json_encode(SerializeMe::serialize($user));

?>


Details

php-serializable

this helps to serialize php object

how to

to use it call the static method called serilaizable which will take in the obect as parameter. see example below


$user = new User();
$user->setFirstname("Theophilus");
$user->setLastname("Omoregbee");

$user->setStates(array(new State("Edo state", "ED"), new State("Lagos State", "LG")));
$user->setCountry(new Country("Nigeria","NG"));

$user->setRoles(array("ADMIN","DB MANAGER"));

$user->setEmail("theo4u@ymail.com");
$user->setPassword("1111");

//lets serialize our object now
echo json_encode(SerializeMe::serialize($user));

which will lead to this output {"firstname":"Theophilus","lastname":"Omoregbee","states":[{"name":"Edo state","code":"ED"},{"name":"Lagos State","code":"LG"}],"country":{"name":"Nigeria","code":"NG"},"dateCreated":1477351778,"roles":["ADMIN","DB MANAGER"],"email":"theo4u@ymail.com","password":"b59c67bf196a4758191e42f76670ceba"}

Response Json

Contributions

Allow contributions to make it faster and re-usable


Screenshots  
  • screenshot
  Files folder image Files  
File Role Description
Files folder image.idea (3 files, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Image file serializable.png Data Auxiliary data
Plain text file Serialize.php Class Class source

  Files folder image Files  /  .idea  
File Role Description
Files folder imagecopyright (1 file)
  Accessible without login Plain text file modules.xml Data Auxiliary data
  Accessible without login Plain text file php-serializable.iml Data Auxiliary data
  Accessible without login Plain text file vcs.xml Data Auxiliary data

  Files folder image Files  /  .idea  /  copyright  
File Role Description
  Accessible without login Plain text file profiles_settings.xml Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 90%
Total:182
This week:0
All time:8,674
This week:121Up