php manual and learn ----- get_called_class get_class

get_class (): Get the current method calls the class name; 
get_called_class (): Gets the class name after the static binding;

abstract class dbObject
{    
    const TABLE_NAME='undefined';
    
    public static function GetAll()
    {
        $c = get_called_class();
        return "SELECT * FROM `".$c::TABLE_NAME."`";
    }    
}

class dbPerson extends dbObject
{
    const TABLE_NAME='persons';
}

class dbAdmin extends dbPerson
{
    const TABLE_NAME='admins';
}

echo dbPerson::GetAll()."<br>";//output: "SELECT * FROM `persons`"
echo dbAdmin::GetAll()."<br>";//output: "SELECT * FROM `admins`"

 

Guess you like

Origin www.cnblogs.com/gaogaoxingxing/p/11113055.html
Recommended