|
| 1 | +# Object |
| 2 | + |
| 3 | +[](https://github.com/josecelano/php-object-literal/releases) |
| 4 | +[](https://travis-ci.org/josecelano/php-object-literal) |
| 5 | +[](https://scrutinizer-ci.com/g/josecelano/php-object-literal) |
| 6 | +[](https://scrutinizer-ci.com/g/josecelano/php-object-literal) |
| 7 | +[](https://packagist.org/packages/josecelano/php-object-literal) |
| 8 | + |
| 9 | +[](mailto:josecelano@gmail.com) |
| 10 | + |
| 11 | +PHP 5.5+ library to create object literals like JavaScript or Ruby. |
| 12 | + |
| 13 | +Creating object literals in PHP is not as easy (or elegant) as in JavaScript or Ruby. |
| 14 | + |
| 15 | +You can create object literals this way: |
| 16 | + |
| 17 | +``` php |
| 18 | +$object = new Object([ |
| 19 | + "name" => "Fido", |
| 20 | + "barks" => true, |
| 21 | + "age" => 10 |
| 22 | +]); |
| 23 | +``` |
| 24 | + |
| 25 | +instead of: |
| 26 | + |
| 27 | +``` php |
| 28 | +$object = new Object(); |
| 29 | +$object->name = 'Fido'; |
| 30 | +$object->barks = true; |
| 31 | +$object->age = 10; |
| 32 | +``` |
| 33 | + |
| 34 | +This class was inspired by these two blog posts: |
| 35 | + |
| 36 | +* https://www.sitepoint.com/php-vs-ruby-lets-all-just-get-along/ |
| 37 | +* https://www.phpied.com/javascript-style-object-literals-in-php/ |
| 38 | + |
| 39 | +In fact, there is am old PHP RFC (2011-06-04) which have not been completely implemented: |
| 40 | + |
| 41 | +* https://wiki.php.net/rfc/objectarrayliterals |
| 42 | + |
| 43 | +This class could be used while the RFC is not implemented. |
| 44 | + |
| 45 | + |
| 46 | +## Install |
| 47 | + |
| 48 | +Via Composer |
| 49 | + |
| 50 | +``` bash |
| 51 | +$ composer require josecelano/php-object-literal |
| 52 | +``` |
| 53 | + |
| 54 | +## Features |
| 55 | + |
| 56 | +- Build from array. |
| 57 | +- Build from json. |
| 58 | +- Build from json with dynamic keys and values. |
| 59 | + |
| 60 | +## Documentation |
| 61 | + |
| 62 | +Please see the [official documentation](http://moneyphp.org). |
| 63 | + |
| 64 | + |
| 65 | +## Testing |
| 66 | + |
| 67 | +I try to follow TDD, as such I use [phpunit](https://phpunit.de) to test this library. |
| 68 | + |
| 69 | +``` bash |
| 70 | +$ composer test |
| 71 | +``` |
| 72 | + |
| 73 | +## TODO |
| 74 | + |
| 75 | +- Add magic getters and setters. |
| 76 | +- Allow to replace variable values in Json like JavaScript: |
| 77 | +From: |
| 78 | +```php |
| 79 | +$object = new Object("{ |
| 80 | + \"name\" : \"" . $valueForName . "\", |
| 81 | + \"barks\" : true, |
| 82 | + \"age\" : 10 |
| 83 | +}"); |
| 84 | +``` |
| 85 | +To: |
| 86 | +```php |
| 87 | +$object = new Object('{ |
| 88 | + "name" : $valueForName, |
| 89 | + "barks" : true, |
| 90 | + "age" : 10 |
| 91 | +}', get_defined_vars()); |
| 92 | +``` |
| 93 | +Replacing `$valueForName` by its value. |
| 94 | +- Allow current invalid PHP json formats. |
| 95 | +```php |
| 96 | +$invalidJson1 = "{ 'bar': 'baz' }"; |
| 97 | +$invalidJson2 = '{ bar: "baz" }'; |
| 98 | +$invalidJson3 = '{ bar: "baz", }'; |
| 99 | +``` |
| 100 | +- Add callable in json format. |
| 101 | +- Allow property value shorthand like ES6: |
| 102 | +```php |
| 103 | +$object = new Object('{ |
| 104 | + $name, |
| 105 | + $barks, |
| 106 | + $age |
| 107 | +}', get_defined_vars()); |
| 108 | +``` |
| 109 | + |
| 110 | +## License |
| 111 | + |
| 112 | +The MIT License (MIT). Please see [License File](LICENSE) for more information. |
0 commit comments