public function getHost() { return $this->host; } } class Model { use \FW\DI\DI; protected $connection = DBConnection::class; protected $table; public function __construct($connection) { } public function getHost() { return $this->connection->getHost(); } } class Model_Post extends Model { protected $table = 'table'; } \FW\DI\AutoBuild::register(Dependancy::class, []); \FW\DI\AutoBuild::register(DBConnection::class, ['host' => 'localhost']); // $dependancy will be autoloaded since it's registered already try { $post = Model_Post::build()->auto(); var_dump($post->getHost()); } catch (Exception $e) { var_dump($e->getMessage()); }
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 $modelImmut = Model_Post::buildImmutable()->with($dbCon);