PHPackages                             adityasetiono/php-functions - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. adityasetiono/php-functions

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

adityasetiono/php-functions
===========================

Function helpers for PHP

5.0.0(8y ago)35603[1 issues](https://github.com/adityasetiono/php-functions/issues)2MITPHPPHP ~7

Since Jul 24Pushed 7y ago4 watchersCompare

[ Source](https://github.com/adityasetiono/php-functions)[ Packagist](https://packagist.org/packages/adityasetiono/php-functions)[ RSS](/packages/adityasetiono-php-functions/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (3)Dependencies (1)Versions (20)Used By (2)

php-functions [![Build Status](https://camo.githubusercontent.com/d0e89aa1121620b01fa51ab4862f4ddfa2170abdb23a42a352ab6f7d3ffb321a/68747470733a2f2f7472617669732d63692e6f72672f616469747961736574696f6e6f2f7068702d66756e6374696f6e732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/adityasetiono/php-functions)
==============================================================================================================================================================================================================================================================================================================================

[](#php-functions-)

A PHP library to serialise/deserialise an array to an object or vice versa.

Installation
============

[](#installation)

To install it with composer:

```
composer require adityasetiono/php-functions
```

Usage
=====

[](#usage)

Import needed functions, for example:

```
use function adityasetiono\util\{deserialize, serialize, generate_alphanumeric};
```

Random String Generator
-----------------------

[](#random-string-generator)

And to use it:

```
$string = generate_alphanumeric(5);

// => "aR4z0"
```

Serialise / Deserialise
-----------------------

[](#serialise--deserialise)

This is a function to serialise/deserialise an array to an object and vice versa. Because the library is built as an external function, the class properties are required to have *getters* and *setters*.

A class example

```
// User.php
class User {
    protected $firstName;
    protected $lastName;
    protected $email;
    protected $username;

    public function setFirstName(string $firstName): User
    {
        $this->firstName = $firstName;

        return $this;
    }

    public function getFirstName(): string
    {
        return $this->firstName;
    }

    public function setLastName(string $lastName): User
    {
        $this->lastName = $lastName;

        return $this;
    }

    public function getLastName(): string
    {
        return $this->lastName;
    }

    public function setEmail(string $email): User
    {
        $this->email = $email;

        return $this;
    }

    public function getEmail(): string
    {
        return $this->email;
    }

    public function setUsername(string $username): User
    {
        $this->username = $username;

        return $this;
    }

    public function getUsername(): string
    {
        return $this->username;
    }
}
```

### Serialiser

[](#serialiser)

```
$user = serialize([
    'firstName' => 'John',
    'lastName' => 'Doe',
    'email' => 'john.doe@example.com',
    'username' => 'johndoe'
], User::class);
```

Result

```
echo $user->getFirstName(); // John
echo $user->getEmail(); // john.doe@example.com
```

Serialised object are compatible with DoctrineORM Entities and can be persisted to database as is (Only tested with Doctrine so far, other ORM Entities need to be tested separately). This is just a generic serialiser to serialise an array to an object.

Here is an example of DoctrineORM Entity Serialization

```
// User.php
use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User {

    /**
     * @ORM\Id
     * @ORM\Column(type="guid")
     * @ORM\GeneratedValue(strategy="UUID")
     */
    protected $uuid;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $firstName;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $lastName;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $email;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $username;

    public function getUuid(): string
    {
        return $this->uuid;
    }

    public function setFirstName(string $firstName): User
    {
        $this->firstName = $firstName;

        return $this;
    }

    public function getFirstName(): string
    {
        return $this->firstName;
    }

    public function setLastName(string $lastName): User
    {
        $this->lastName = $lastName;

        return $this;
    }

    public function getLastName(): string
    {
        return $this->lastName;
    }

    public function setEmail(string $email): User
    {
        $this->email = $email;

        return $this;
    }

    public function getEmail(): string
    {
        return $this->email;
    }

    public function setUsername(string $username): User
    {
        $this->username = $username;

        return $this;
    }

    public function getUsername(): string
    {
        return $this->username;
    }
}
```

### Serialiser

[](#serialiser-1)

Uuid is a primary key and auto-generated by the DB.

```
$user = serialize([
    'firstName' => 'John',
    'lastName' => 'Doe',
    'email' => 'john.doe@example.com',
    'username' => 'johndoe'
], User::class);
```

Persist to DB

```
// obtaining the entity manager
$entityManager = EntityManager::create($conn, $config);
$entityManager->persist($user);
$entityManager->flush();
```

### Deserialiser

[](#deserialiser)

Getting the object/entity from Database

```
$uuid = 'e48a0ba1-1504-11e8-ae97-0649b2727d37'; // Hardcoded for display purposes
$user = $entityManager->find(User::class, $uuid);
```

Use the deserialise

```
$array = deserialise($user); // empty option means complete deserialization by default.
echo json_encode($array);
```

Content of `$array` will be

```
{
  "uuid": "e48a0ba1-1504-11e8-ae97-0649b2727d37",
  "firstName": "John",
  "lastName": "Doe",
  "email": "john.doe@example.com",
  "username": "johndoe"
}
```

Another example with custom fields. The second parameter accepts an associative array with key which will be the result's key and value is the property name. The deserialiser expects the value to be exactly the same as camelCase of the `getter` without `get`.

```
$customArray = deserialize($user,[
    'first_name' => 'firstName',
    'surname' => 'lastName',
    'email' => 'email',
]);
echo json_encode($customArray);
```

Content of `$customArray` will be

```
{
  "first_name": "John",
  "surname": "Doe",
  "email": "john.doe@example.com"
}
```

The deserialiser should work with nested object and array of objects. Further example will be provided in the documentation. (It is available in the tests files).

Issues
======

[](#issues)

Feel free to use GitHub Issues or email directly to `winged.zach@gmail.com` as I started this library on my own. Collaborators are welcomed.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance13

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 84.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~13 days

Recently: every ~31 days

Total

19

Last Release

2985d ago

Major Versions

1.5.1 → 2.0.02017-10-04

2.0.2 → 3.0.02017-11-03

3.0.1 → 4.0.02017-11-06

4.0.2 → 5.0.0-RC12017-12-04

### Community

Maintainers

![](https://www.gravatar.com/avatar/a4f49ab73e1481d1f9a278d31a7bad37487c982229f2bc9b989c98eb9bea190c?d=identicon)[adityasetiono](/maintainers/adityasetiono)

---

Top Contributors

[![adityasetiono](https://avatars.githubusercontent.com/u/4496987?v=4)](https://github.com/adityasetiono "adityasetiono (37 commits)")[![tharindudoo](https://avatars.githubusercontent.com/u/17094609?v=4)](https://github.com/tharindudoo "tharindudoo (4 commits)")[![hc-12](https://avatars.githubusercontent.com/u/17104732?v=4)](https://github.com/hc-12 "hc-12 (1 commits)")[![indikarajanayake](https://avatars.githubusercontent.com/u/1362022?v=4)](https://github.com/indikarajanayake "indikarajanayake (1 commits)")[![t49tran](https://avatars.githubusercontent.com/u/2223362?v=4)](https://github.com/t49tran "t49tran (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/adityasetiono-php-functions/health.svg)

```
[![Health](https://phpackages.com/badges/adityasetiono-php-functions/health.svg)](https://phpackages.com/packages/adityasetiono-php-functions)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
