A PHP class helper

Installs: 178

Dependents: 1

Stars: 3

Watchers: 2

Forks: 2

Open Issues: 1

Language: PHP

dev-master 2014-05-25 15:52 UTC


Build Status Scrutinizer Quality Score Code Coverage SensioLabsInsight

What is TRex?

TRex is a light toolbox with low level classes implemented with standard missing features. It provides a set of tools for better programming.

How to start?

With Composer

You just have to add the TRex package in your composer.json and update your project.

"require": {
    "raphhh/trex": "dev-master"

Without Composer

The simplest way to start is to include the bootstrap file in your working file.

include_once 'trex/src/TRex/boostrap.php';

//your code here

What can I do?

In extending TRex, you have build-in features to use in your classes.

Some examples:

Object hydration from JSON

You can hydrate an object from a JSON string.

class Foo extends TRex/Core/Object{

    private $a;

    function getA(){
        return $this->a;

    function setA($a){
        $this->a = a;

$foo = new Foo('{"a": 123}');
echo $foo->getA(); //123

Object export in JSON

You can easily convert your object to JSON, choosing which property you want to export, including private ones. If you want to exclude some properties, you can add them a comment tag @transient.

class Foo extends TRex/Core/Object{

    private $a = 'a';

     * @transient
    public $b = 'b';

echo new Foo(); //{a: "a"} => only non transient properties

Composite action on several objects

You can list objects and call a same method on all of them.

class Foo extends TRex/Core/Object{

    private $a = 'a';

    public getA(){
        return $this->a;

    public setA($a){
        $this->a = $a;

//create some objects
$foo1 = new Foo();
$foo2 = new Foo();
//getA() returns "a"
echo $foo1->getA(); //a
echo $foo2->getA(); //a
//composite action sets "b" instead of "a"
(new Objects(array($foo1, $foo2)))->setA('b');
//getA() returns "b", the value set by the composite action
echo $foo1->getA(); //b
echo $foo2->getA(); //b

Memoization of method result

You can cache your method result without using a local property.

class Foo extends TRex/Cache/CacheObject{

    private $a = 'a';

    public concat($arg){
        return $this->cache(function($arg){
            echo 'A lot of things...'
            return $this->a.$arg; //private context of your current object is still present.

$foo = new Foo();

//first call, we execute the function
$result = $foo->concat('b'); // A lot of things...
echo $result; //ab
//second call, nothing is written <= the function has not been called.
$result = $foo->concat('b');
echo $result; //ab
//if we call the method with another argument, this is another cache.
$result = $foo->concat('c'); // A lot of things...
echo $result; //ac
//but first cache is still pending.
$result = $foo->concat('b');
echo $result; //ab