Git Product home page Git Product logo

trex's Introduction

README

Build Status Scrutinizer Quality Score Code Coverage

What is TRex?

TRex is a light toolbox with low level classes implemented with standard missing features.

How to start?

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 witch property you what to export with @transient comment tag, including private ones.

class Foo extends TRex/Core/Object{

    private $a = 'a';

    /**
     * @transient
     */
    public $b = 'b';

}

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

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() return "a"
echo $foo1->getA(); //a
echo $foo2->getA(); //a

//composite action set "b" instead of "a"
(new Objects(array($foo1, $foo2)))->setA('b');

//getA() return "b", the value setted 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

Resources

For documentation, see TRex/docs/

trex's People

Contributors

raphhh avatar

Watchers

Nicolas De Boose avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.