php,静态方法重载

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?

You should avoid use of static calls in general [article].

And in this case, you are even making it worse, because you clearly expect two different behavior from same function. This is reason enough to have two separate functions. Besides, there is no "more elegant" way to do it. Only thing you can change there is to assume, that whenever function is called with a parameter, it is static. Other alternatives would either include reflections (which are slow) or debug_backtrace() (which is plain ugly).

I can recommend for you to watch few of the lectures from "Clean Code Talks". Even if this does not convince you to change your style, it will give you a solid introduction in concepts like Dependency Injection and Unit Testing:

Also, you really should stop using the old mysql_* functions as an API for access MySQL. They are more then 10 years old, not maintained anymore and community has begun the process of deprecation.

You should take a hard look at the alternatives: PDO and MySQLi. They both provide ability to use prepared statements.

A more elegant way might be to isolate the group_id => group_name as a static method. Than just reuse said method

<?php
class MYOBJECT
{
  private $group_id;

  public function SetGroupId($_id) { $this->group_id = $_id; }

  public function GetGroupName() { 
    return self::GetGroupNameByID($this->group_id);
  }
  public function GetGroupId() { return $this->group_id; }

  public static function GetGroupNameByID($_id)
  {
    // Check if $_id is MYOBJECT
    $id = $_id instanceof self ? $_id->GetGroupID() : $_id;

    $query mysql_query("SELECT name FROM group WHERE id = $id");
    list($name) = mysql_fetch_array($query);

    return $name;
  }
}

$name = MYOBJECT::GetGroupNameByID(4);
$name = MYOBJECT::GetGroupNameByID($obj);
$name = $object->GetGroupName();