https://github.com/josecelano/php-object-literal
Factory class for creating PHP object literals
https://github.com/josecelano/php-object-literal
Last synced: 2 months ago
JSON representation
Factory class for creating PHP object literals
- Host: GitHub
- URL: https://github.com/josecelano/php-object-literal
- Owner: josecelano
- License: mit
- Created: 2017-03-20T22:15:42.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-03-24T23:33:51.000Z (about 8 years ago)
- Last Synced: 2025-04-01T21:54:38.110Z (2 months ago)
- Language: PHP
- Size: 7.81 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Object
[](https://github.com/josecelano/php-object-literal/releases)
[](https://travis-ci.org/josecelano/php-object-literal)
[](https://scrutinizer-ci.com/g/josecelano/php-object-literal)
[](https://scrutinizer-ci.com/g/josecelano/php-object-literal)
[](https://packagist.org/packages/josecelano/php-object-literal)[](mailto:[email protected])
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:
```php
$object = new Object([
"name" => "Fido",
"barks" => true,
"age" => 10
]);
```or
```php
$object = new Object([
"name" => "Fido",
"barks" => true,
"age" => 10,
'say' => function ($self) {
if ($self->barks) {
return "Woof";
}
return "";
}
]);
```or
```php
$object = new Object('{
"name" : "Fido",
"barks" : true,
"age" : 10
}');
```instead of:
```php
$object = new Object();
$object->name = 'Fido';
$object->barks = true;
$object->age = 10;
```This class was inspired by these two blog posts:
* https://www.sitepoint.com/php-vs-ruby-lets-all-just-get-along/
* https://www.phpied.com/javascript-style-object-literals-in-php/In fact, there is am old PHP RFC (2011-06-04) which have not been completely implemented:
* https://wiki.php.net/rfc/objectarrayliterals
This class could be used while the RFC is not implemented.
## Install
Via Composer
```bash
$ composer require josecelano/php-object-literal
```## Features
- Build from array.
- Build from json.
- Build from json with dynamic keys and values.## Testing
I try to follow TDD, as such I use [phpunit](https://phpunit.de) to test this library.
```bash
$ composer test
```## TODO
- Add magic getters and setters.
- Allow to replace variable values in Json like JavaScript:
From:
```php
$object = new Object("{
\"name\" : \"" . $valueForName . "\",
\"barks\" : true,
\"age\" : 10
}");
```
To:
```php
$object = new Object('{
"name" : $valueForName,
"barks" : true,
"age" : 10
}', get_defined_vars());
```
Replacing `$valueForName` by its value.
- Allow current invalid PHP json formats.
```php
$invalidJson1 = "{ 'bar': 'baz' }";
$invalidJson2 = '{ bar: "baz" }';
$invalidJson3 = '{ bar: "baz", }';
```
- Add callable in json format.
- Allow property value shorthand like ES6:
```php
$object = new Object('{
$name,
$barks,
$age
}', get_defined_vars());
```## License
The MIT License (MIT). Please see [License File](LICENSE) for more information.