PHP code example of jgswift / persistr

1. Go to this page and download the library: Download jgswift/persistr 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 / persistr example snippets



class MyUser implements persistr\Interfaces\Persistent {
    use persistr\Persistent;

}

$user = new MyUser;

$model = $user->getModel();

$model->bind('foo',function() {
    return 'bar';
});

$value = $user->foo;

var_dump($value); // returns 'bar'

// MODEL CLASS
class MyUserModel implements persistr\Interfaces\Model {
    private $className;
    private static $registry;

    function __construct($className) {
        $this->className = $className;
        if(empty(self::$registry)) {
            self::$registry = new persistr\Object\Registry($this,$className);
        }
    }

    public function getClassName() {
        return $this->className;
    }

    public function getRegistry() {
        return self::$registry;
    }

    public function bind($attribute, callable $callable=null) {
        persistr\Object\Binding\Registry::bind(self::$registry->getTypeName(), $attribute, $callable);
        return $this;
    }

    public function bindTo($object,$attribute,callable $callable=null) {
        persistr\Object\Binding\Registry::bindTo($object, $attribute, $callable);
        return $this;
    }
}

// PERSISTENCE REGISTRATION
$persistor = new persistr\Persistor('MyUser');
persistr\Registry::register($persistor);

$model = new MyUserModel('MyUser');

$persistor->getDataSource()->insert('MyUser',$model);