There is an object, which can be initialized by id or by name.
How it should be handled?
class user
{
function __construct($id_or_name)
{
if ( is_numeric($id_or_name) )
{
$this->id = $id_or_name;
$this->populate_by_id($this->id)
}
else
{
$this->name = $id_or_name;
$this->populate_by_name($this->name)
}
}
...
}
$user1 = new user('Max');
$user2 = new user(123);
Is it ok for general practice?
IMHO, it’s terrible. Introuce two static “fabric methods”, one receive
string, other –integer. And construct your object differently: