- var_dump: documentation ( source)
- get_object_vars: documentation ( source)
<?php
trait CastableBehavior {
public static function getFromContext($obj) {
foreach (get_object_vars($obj) as $key => $value) {
$this->$key = $value;
}
}
}
interface UserTypes {
const USER = 1;
const ADMIN = 2;
const MODERATOR = 3;
}
class User {
private $id;
private $name;
private $type;
public function __construct($id, $name, $type) {
$this->id = $id;
$this->name = $name;
$this->type = $type;
}
public function getID() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function getType() {
return $this->type;
}
}
class Admin {
use CastableBehavior;
private $id;
private $name;
private $type;
}
$user = new User(2, 'John', UserTypes::ADMIN);
if ($user->getType() === UserTypes::ADMIN) {
$admin = Admin::getFromContext($user);
}
var_dump($admin);