简体   繁体   中英

Get constant overridden from child in method declared within an abstract class

I have this (shortened):

abstract class MyModel
{
    const DBID = "";
    const TOKEN = "";

    public function getDB()
    {
        if(!$this->_DB)
        {
            $c = get_called_class(); // doesn't work pre php 5.3
            echo $c::DBID; // doesn't work pre php 5.3
            echo $c::TOKEN // doesn't work pre php 5.3
        }

        return $this->_qb;
    } 

The problem is that get_called_class() and the $c::DBID/TOKEN doesn't work in php < 5.3

Is there a way I can accomplish the same task from within the abstract class that is compatible with 5.2.9?

EDIT: Constants aren't really meant to be changed throughout object instantiations, you may want to consider member variables instead.

Abstract classes cannot be instantiated directly. You could create a child class to extend your abstract class, then make the call to getDb().

abstract class MyModel
{
    private $dbId;
    private $token;

    public function setDbId($dbId)
    {
        $this->dbId = $dbId;
    }

    public function setToken($token)
    {
        $this->token = $token;
    }

    public function getDbId()
    {
        return $this->dbId;
    }

    public function getToken()
    {
        return $this->token;
    }

    public function __construct()
    {
        // All child classes will have the same values
        $this->setDbId('myParentDbId');
        $this->setToken('myParentToken');
    }

    protected function getDb()
    {
        echo $this->getDbId();
        echo $this->getToken();
    }
}

class MyChildModel extends MyModel
{
    // Don't need any methods, just access to abstract parent
    // But if I wanted to override my parent I could do this
    public function __construct()
    {            
        parent::__construct();

        $this->setDbId('myChildDbId');
        $this->setToken('myChildToken');
    }
}

$myChildModel = new MyChildModel();
var_dump($myChildModel->getDb());

I had a solution in a PHP 5.2 codebase that used reflection to get at the constants of a subclass from a superclass, but I'd advise against doing that unless it's absolutely necessary because reflection is q relatively expensive tool in PHP in terms of performance.

PHP 5.3 introduced the static keyword as in static::CONST instead of self::CONST to access static members of a class. I've never actually tried it but I believe it should be able to do what you need. Look up late static binding in the PHP manual.

For the record, here's the code for a method that used reflection to get a subclass constant.

class SomeClass
{
    /**
     * Get reflection class for item
     * 
     * Get a reflector for this item so that internal constants can be used for the permission checking
     * functions.  This is necessary because of how static binding works prior to PHP 5.3.  
     * 
     * @return ReflectionClass
     */
    protected function getRef ()
    {
        if (!$this -> ref)
        {
            $this -> ref    = new ReflectionClass ($this);
        }
        return ($this -> ref);
    }

    /**
     * Check that the user has permission to create an item of the type this object represents
     *
     * @todo Use late static binding instead of reflection once PHP 5.3 becomes available on the server
     * @return bool True if OK
     */
    public function canCreate ()
    {
        $ref    = $this -> getRef ();
        if ($flag = $ref -> getConstant ('USR_FLAG_CREATE'))
        {
            return (self::$user -> permissions [$flag]);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM