PHP code example of jgswift / qtil

1. Go to this page and download the library: Download jgswift/qtil library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jgswift / qtil example snippets



class Foo implements \ArrayAccess, \Countable, \IteratorAggregate
{
    use qtil\ArrayAccess, qtil\Countable, qtil\IteratorAggregate;
}

$foo = new Foo;
$foo['bar'] = 'baz';
var_dump($foo['bar']); // returns "baz"


class Foo
{
    use qtil\ObjectAccess;
}

$foo = new Foo;
$foo->bar = 'baz';
var_dump($foo->bar); // returns "baz"


class Foo
{
    use qtil\ObjectAccess, qtil\JSONAccess;
}

$foo = new Foo;
$foo->bar = 'baz';

$json_string = $foo->toJSON();

$foo2 = new Foo;
$foo2->fromJSON($json_string);

var_dump($foo2->bar); // returns "baz"


class Foo implements \Serializable
{
    use qtil\ObjectAccess, qtil\Serializable;
}

$foo = new Foo;
$foo->bar = 'baz';

$serial_string = serialize($foo);

$foo2 = unserialize($serial_string);

var_dump($foo2->bar); // returns "baz"


class Foo
{
    use qtil\ObjectAccess, qtil\Iterator;
}

$foo = new Foo;
$foo->bar = 'baz';
$foo->baz = 'rus';

foreach($foo as $name => $value) {
    var_dump($name,$value); // returns bar => baz, then baz => rus
}


class Foo
{
    use qtil\ObjectAccess, qtil\IteratorAggregate;
}

$foo = new Foo;
$foo->bar = 'baz';
$foo->baz = 'rus';

foreach($foo as $name => $value) {
    var_dump($name,$value); // returns bar => baz, then baz => rus
}

class Foo
{
    use qtil\ObjectAccess, qtil\IteratorAggregate;

    public static $ITERATOR_CLASS = 'FooIterator';
}

class FooIterator implements \Iterator {
    function current() { /* ... */ }
    function key() { /* ... */ }
    function next() { /* ... */ }
    function rewind() { /* ... */ }
    function valid() { /* ... */ }
}

$foo = new Foo;

foreach($foo as $value) {
    // iterates over foo using fooiterator
}

class Foo
{
    use qtil\ObjectAccess, qtil\Generator;
}

$foo = new Foo([
    'bob',
    'sam',
    'jim'
]);

$foo->setGenerator(function($item) {
    return $item;
});

$gen = $foo->getGenerator();

foreach($gen as $item) {
    // 'bob' , 'sam' , 'jim'
}


class Foo
{

}

$foo = new Foo();
$id = qtil\Identifier::identify($foo); // Returns an unique hash

class Foo
{
    function getID() {
        return qtil\Identifier::identify($this);
    }
}

$foo = new Foo();
$id = $foo->getID(); // Returns an unique hash

class Foo
{
    function getID() {
        return qtil\Identifier::identify($this);
    }

    function getUniqueID() {
        return 1;
    }
}

qtil\Identifier::addScheme(
    new qtil\Identifier\Scheme\MethodScheme('getUniqueID')
);

$foo = new Foo();
$id = $foo->getID(); // Returns 1

class Foo
{
    function getID() {
        return qtil\Identifier::identify($this);
    }
}

qtil\Identifier::addScheme(
    new qtil\Identifier\Scheme\ClassScheme('Foo',function() {
        return 1;
    })
);

$foo = new Foo();
$id = $foo->getID(); // Returns 1

class User {
    /* ... */
}

class MyUserFactory { 
    use qtil\Factory;
}

$factory = new MyUserFactory;
$user = $factory->make('User');

var_dump(get_class($user)); // 'User'


class MyUserProxy {
    use qtil\Proxy;
}

class User {
    /* ... */

    function sayHello() {
        return 'hello';
    }
}

$user = new User;

$proxy = new MyUserProxy();
$proxy->setSubject($user);

$message = $proxy->sayHello();

var_dump($message); // 'hello'


class MyBinarySwitch {
    use qtil\Executable;
    
    function execute($start) {
        if($start) {
            return false;
        }
        
        return true;
    }
}

$switch = new MyBinarySwitch();

$result = $switch(false);   // starts off

var_dump($result);          // ends on

$result = $switch(true);    // starts on

var_dump($result);          // ends off

class Query {
    use qtil\Chain;
}

// a namespace with the same signature as the class must be created
// or alternatively the namespaces may be configured manually
namespace Query {
    class Select {
        function __construct() {
            /* Specify relevant fields */
        }
    }

    class From {
        function __construct() {
            /* Choose a source */
        }
    }
}

namespace OtherQueryTypes {
    class Within {
        function __construct() {
            /* Specify constraints */
        }
    }
}

// create a query using above chain classes
$query = new Query;

$query->select()->from();

var_dump(count($query->getLinks())); // 2

// this namespace extension will only extend this individual query instance
$query->addNamespace("OtherQueryTypes");

$query->within();

var_dump(count($query->getLinks())); // 3

// add a second namespace globally to extend every instantiated object of this type.
qtil\Chain\ClassRegistry::addNamespace('Query','OtherQueryTypes');
sh
php composer.phar