I have an object with method that sometimes I need to call as static, sometimes not.
class MYOBJECT
{
private $group_id;
public function SetGroupId($_id) { $this->group_id = $_id; }
public static function GetGroupName($_id=NULL)
{
// is there any way to implement condition like this?
if( _called_as_static ) $id = $_id;
else $id = $this->group_id;
$query mysql_query("SELECT name FROM group WHERE id = $id");
list($name) = mysql_fetch_array($query);
return $name;
}
}
$obj = new MYOBJECT;
$obj->SetGroupId(4);
// should work both ways
$name = $obj->GetGroupName();
$name = MYOBJECT::GetGroupName(4);
I solved it this way:
public static function MYOBJECT::GetGroupName($_id=NULL)
{
if( is_object($_id) ) $_id = $_id->GetGroupId();
...
}
$name = MYOBJECT:GetGroupName(4);
$name = $obj->GetGroupName($obj);
but still, is there something more elegant?