-
just extend this class and set attributes and methods you need.
use miladm\DataObject; class User extends DataObject { public string $name; public int $age; public string $email; public function validateEmail() { return $this->email == 'miladmohebnia@gmail.com'; } public function validateName() { return strlen($this->name) >= 4; } public function validate() { return $this->validateName() && $this->validateEmail(); } }
-
create init method for setting actions happen before object constructs.
class User extends DataObject { public object $profile; public object $feed; public function init() { $this->profile = new Profile(); $this->feed = Feed::class; } }
-
you can add data both with
constructor
andinjectMethod
-
using constructor
class User extends DataObject { .... } $user = UserModel::get_by_id(12); $u = new User($user);
-
using
injectMethod
class User extends DataObject { .... } $u = new User(); $user = UserModel::get_by_id(12); $u->injectData($user);
-
class User extends DataObject
{
private $model;
public function init()
{
$this->model = UserModel::class;
}
public function loadUser_byId(int $id)
{
$this->injectData($this->model::where(['id' => $id])->getOne());
}
}
$user = new User;
$user->loadUser_byId(12);
// code above used as alternative to code below
// $user = UserModel::get_by_id(12);
// $u = new User($user);