Пример #1
0
{
    use \FW\DI\DI;
    public $id;
    public $title;
    public $content;
    protected $table = 'post';
    public function __toString()
    {
        return "Model_Post ({$this->table}) {$this->id}";
    }
    public function setId($i)
    {
        $this->id = $i;
    }
}
$dbCon = DBConnection::build()->with(['host' => 'localhost', 'user' => 'root', 'password' => 'pwd']);
$model = Model_Post::build()->with($dbCon);
var_dump($dbCon->isConnected());
// true
var_dump($model);
$model->id = 42;
var_dump($model->id);
// 42
var_dump((string) $model);
// Model_Post (post) 42
var_dump(isset($model->title));
// false
var_dump($model->title);
// null
var_dump("#### Immutable");
// Once build it can't be changed externally
Пример #2
0
    protected $user;
    protected $password;
    public function __construct($host)
    {
    }
}
class DBExtend extends DBConnection
{
}
class Model
{
    use \FW\DI\DI;
    protected $connection = DBExtend::class;
    protected $table;
    public function __construct($connection)
    {
    }
    public function hello()
    {
        return 'hello';
    }
}
$model = Model::build()->with(DBExtend::build()->with('localhost', 'host'));
echo $model->hello() . "\n";
try {
    $model = Model::build()->with(DBConnection::build()->with('localhost', 'host'));
    // This will fail
    echo $model->hello() . "\n";
} catch (Exception $e) {
    var_dump($e->getMessage());
}
Пример #3
0
<?php

require __DIR__ . '/../vendor/autoload.php';
error_reporting(E_ALL);
class DBConnection
{
    use \FW\DI\DI;
    protected $host;
    protected $user;
    protected $password;
    public function __construct($host, $user, $password)
    {
    }
    public function isConnected()
    {
        return true;
    }
}
// As an array of named parameters
$dbCon = DBConnection::build()->with(['host' => 'localhost', 'user' => 'root', 'password' => 'pwd']);
// OR a list of chained parameters
$dbCon = DBConnection::build()->with('localhost', 'host')->with('root', 'user')->with('pwd', 'password');
var_dump($dbCon);
Пример #4
0
<?php

require __DIR__ . '/../vendor/autoload.php';
error_reporting(E_ALL);
class DBConnection
{
    use \FW\DI\DI;
    protected $host;
    public function __construct($host)
    {
    }
    public function hello()
    {
        return 'hello';
    }
}
$model = DBConnection::build()->with('localhost', 'host');
echo $model->hello() . "\n";
try {
    $model->with('test', 'host');
    // This will fail
    echo $model->hello() . "\n";
} catch (Exception $e) {
    var_dump($e->getMessage());
}
Пример #5
0
    {
    }
}
class DBConnection
{
    use \FW\DI\DI;
    public $host;
    public function __construct($host)
    {
    }
}
class Model
{
    use \FW\DI\DI;
    public $connection = DBConnection::class;
    public $dependancy = Dependancy::class;
    public function __construct($connection, $dependancy)
    {
    }
}
\FW\DI\AutoBuild::register(Dependancy::class, ['name' => 'AutoName']);
\FW\DI\AutoBuild::register(DBConnection::class, ['host' => 'localhost']);
// $dependancy will be autoloaded since it's registered already
try {
    $post = Model::build()->auto();
    var_dump($post->connection->host);
    $overridenPost = Model::build()->with(DBConnection::build()->with('127.0.0.1', 'host'), 'connection')->auto();
    var_dump($overridenPost->connection->host);
} catch (Exception $e) {
    var_dump($e->getMessage());
}