I am facing a serious design problem that is driving me crazy. I think it can only be solved with multiple inheritance or something. So here is what I want to do:
Say I have a basic user class called OrdinaryUser defined in this way:
class OrdinaryUser
{
private $id,$name;
public function __construct($id)
{
$this->id = $id;
$this->name = fictionalDB::getUserNameById($id);
}
public function getName()
{
return $this->name;
}
public function getId()
{
return $this->id;
}
}
And I have a subclass called AdminUser with additional functionality:
class AdminUser extends OrdinaryUser
{
public function deleteUser($id)
{
echo "Deleting user where id=$id";
}
}
The problem: What if I have already instantiated an object of type "OrdinaryUser" and want to make it into an AdminUser object on-the-fly? Is there a way of "extending objects" so to avoid instantiating a subclass and having to re-populate the new object's fields with the same data?
Another related problem: I might have many other categories of users defined later, each having their own unique bahaviour, but always a basic one, and it wouldn't make sense to create a hierarchy in this case because most times one type of object should not be inheriting methods from the other type, although it might be desirable to have additional functionality from one type being "imported" into the other dinamically.