PHPackages                             josecelano/php-object-literal - 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. josecelano/php-object-literal

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

josecelano/php-object-literal
=============================

Factory class for creating PHP object literals

26PHP

Since Mar 24Pushed 9y ago1 watchersCompare

[ Source](https://github.com/josecelano/php-object-literal)[ Packagist](https://packagist.org/packages/josecelano/php-object-literal)[ RSS](/packages/josecelano-php-object-literal/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

Object
======

[](#object)

[![Latest Version](https://camo.githubusercontent.com/5873cdd9a6f15b740030843ff3ebef75ed4d415a8358bd426cb1f3018c387082/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6a6f736563656c616e6f2f7068702d6f626a6563742d6c69746572616c2e7376673f7374796c653d666c61742d737175617265)](https://github.com/josecelano/php-object-literal/releases)[![Build Status](https://camo.githubusercontent.com/007d90e0fd5b983b9efc1d8f2961e9824f10269baf719f7dc0b1c59eef384d19/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6a6f736563656c616e6f2f7068702d6f626a6563742d6c69746572616c2e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/josecelano/php-object-literal)[![Code Coverage](https://camo.githubusercontent.com/b4c4ff058d0d92c1dd2969c43d10795d47ee431cb4a33de3aafe8627e34fe130/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6a6f736563656c616e6f2f7068702d6f626a6563742d6c69746572616c2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/josecelano/php-object-literal)[![Quality Score](https://camo.githubusercontent.com/54f66d3ec4b46ac748dfea845dba796499d8c74935875dba1aebeb3f4bfa087d/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6a6f736563656c616e6f2f7068702d6f626a6563742d6c69746572616c2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/josecelano/php-object-literal)[![Total Downloads](https://camo.githubusercontent.com/e903f8bbcd59d77223f892776558c4fc70c91bb30ad3a7637d7f0613d926b3d1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a6f736563656c616e6f2f7068702d6f626a6563742d6c69746572616c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/josecelano/php-object-literal)

[![Email](https://camo.githubusercontent.com/4d0e47b6f6a563db1220e1fd3c83572fe0e47400375c7779458ae007b3311cdf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f656d61696c2d6a6f736563656c616e6f40676d61696c2e636f6d2d626c75652e7376673f7374796c653d666c61742d737175617265)](mailto:josecelano@gmail.com)

PHP 5.5+ library to create object literals like JavaScript or Ruby.

Creating object literals in PHP is not as easy (or elegant) as in JavaScript or Ruby.

You can create object literals this way:

```
$object = new Object([
    "name" => "Fido",
    "barks" => true,
    "age" => 10
]);
```

or

```
$object = new Object([
    "name" => "Fido",
    "barks" => true,
    "age" => 10,
    'say' => function ($self) {
        if ($self->barks) {
            return "Woof";
        }
        return "";
    }
]);
```

or

```
$object = new Object('{
    "name" : "Fido",
    "barks" : true,
    "age" : 10
}');
```

instead of:

```
$object = new Object();
$object->name = 'Fido';
$object->barks = true;
$object->age = 10;
```

This class was inspired by these two blog posts:

-
-

In fact, there is am old PHP RFC (2011-06-04) which have not been completely implemented:

-

This class could be used while the RFC is not implemented.

Install
-------

[](#install)

Via Composer

```
$ composer require josecelano/php-object-literal
```

Features
--------

[](#features)

- Build from array.
- Build from json.
- Build from json with dynamic keys and values.

Testing
-------

[](#testing)

I try to follow TDD, as such I use [phpunit](https://phpunit.de) to test this library.

```
$ composer test
```

TODO
----

[](#todo)

- Add magic getters and setters.
- Allow to replace variable values in Json like JavaScript: From:

```
$object = new Object("{
    \"name\" : \"" . $valueForName . "\",
    \"barks\" : true,
    \"age\" : 10
}");
```

To:

```
$object = new Object('{
    "name" : $valueForName,
    "barks" : true,
    "age" : 10
}', get_defined_vars());
```

Replacing `$valueForName` by its value.

- Allow current invalid PHP json formats.

```
$invalidJson1 = "{ 'bar': 'baz' }";
$invalidJson2 = '{ bar: "baz" }';
$invalidJson3 = '{ bar: "baz", }';
```

- Add callable in json format.
- Allow property value shorthand like ES6:

```
$object = new Object('{
    $name,
    $barks,
    $age
}', get_defined_vars());
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

20

—

LowBetter than 13% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/58816?v=4)[Jose Celano](/maintainers/josecelano)[@josecelano](https://github.com/josecelano)

### Embed Badge

![Health badge](/badges/josecelano-php-object-literal/health.svg)

```
[![Health](https://phpackages.com/badges/josecelano-php-object-literal/health.svg)](https://phpackages.com/packages/josecelano-php-object-literal)
```

###  Alternatives

[symfony/service-contracts

Generic abstractions related to writing services

2.6k914.7M564](/packages/symfony-service-contracts)[henck/rtf-to-html

RTF to HTML converter in PHP

107837.5k1](/packages/henck-rtf-to-html)

PHPackages © 2026

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