سؤال

لدي الهيكل الأساسي الحالي لكل كائن مجال أحتاج إلى إنشاءه:

class Model_Company extends LP_Model
{   
    protected static $_gatewayName = 'Model_Table_Company';
    protected static $_gateway;
    protected static $_class;

    public static function init()
    {
        if(self::$_gateway == null)
        {
            self::$_gateway = new self::$_gatewayName();
            self::$_class = get_class();
        }
    }

    public static function get() 
    {
        self::init();

        $param = func_get_arg(0);

        if($param instanceof Zend_Db_Table_Row_Abstract)
        {
            $row = $param;
        }
        elseif(is_numeric($param))
        {
            $row = self::$_gateway->find($param)->current();
        }

        return new self::$_class($row);
    }

    public static function getCollection()
    {
        self::init();

        $param = func_get_arg(0);

        if($param instanceof Zend_Db_Table_Rowset_Abstract)
        {
            $rowset = $param;
        }
        elseif(!$param)
        {
            $rowset = self::$_gateway->fetchAll();
        }

        $array = array ();      

        foreach ($rowset as $row)
        {
            $array[] = new self::$_class($row);
        }

        return $array;
    }
}

حاولت في البداية إعادة تكوين الأساليب الساكنة في فئة LP_MODEL الأصل فقط للتعلم أخيرا ما يعنيه ما "الربط الثابت المتأخر" في عالم PHP.

أنا فقط أتساءل عما إذا كان لدى أي شخص اقتراحات حول كيفية إعادة تكوين هذا الرمز حتى لا أتصل ب redeclare نفس الوظائف الثلاث في كل كائن مجال أقوم بإنشائه؟

هل كانت مفيدة؟

المحلول

وماذا عن هذا:

<?php

abstract class Model_Abstract
{
    protected $_gatewayName = null;
    protected $_gateway = null;

    protected function _init()
    {
        $this->_gateway = new $this->_gatewayName();
    }

    protected function __construct($row = null)
    {
        $this->_init();
        if ($row) {
            $this->_data = $row;
        }
    }

    public static function getAbstract($class, $param)
    {
        $model = new $class();
        if($param instanceof Zend_Db_Table_Row_Abstract)
        {
                $row = $param;
        }
        elseif(is_numeric($param))
        {
                $row = $model->_gateway->find($param)->current();
        }

        return new $class($row);
    }

    public static function getAbstractCollection($class, $param = null)
    {
        $model = new $class();
        if($param instanceof Zend_Db_Table_Rowset_Abstract)
        {
                $rowset = $param;
        }
        elseif($param === null)
        {
                $rowset = $model->_gateway->fetchAll();
        }

        $array = array ();

        foreach ($rowset as $row)
        {
                $array[] = new $class($row);
        }

        return $array;
    }

    abstract public static function get($param);
    abstract public static function getCollection($param = null);
}

class Model_Company extends Model_Abstract
{
    protected $_gatewayName = 'Model_Table_Company';

    public static function get($param) {
        return self::getAbstract(__CLASS__, $param);
    }

    public static function getCollection($param = null) {
        return self::getAbstractCollection(__CLASS__, $param);
    }
}

class Model_Table_Company extends Zend_Db_Table_Abstract
{
    protected $_name = 'company';
}

$model = Model_Company::get(1);
print "Got an object of type ".get_class($model)."\n";

$models = Model_Company::getCollection();
print "Got ".count($models)." objects of type ".get_class($models[0])."\n";

?>

لسوء الحظ، لجعل الوظائف سهلة الاتصال، يجب عليك تكرار get() و getCollection() في كل فرعية. الخيار الآخر هو استدعاء الوظيفة في الفئة الأصل:

$model = Model_Abstract::getAbstract('Model_Company', 1);
print "Got an object of type ".get_class($model)."\n";

$models = Model_Abstract::getAbstractCollection('Model_Company');
print "Got ".count($models)." objects of type ".get_class($models[0])."\n";

يمكنك إعادة تسمية الفئة الأساسية وأسماء وظائفها إذا كنت ترغب في الذهاب إلى هذا الطريق. لكن النقطة هي أن يجب عليك تسمية فئة الطفل في مكان واحد أو آخر: إما أن تصنع وظيفة غنيمة في فئة الطفل كما هو الحال في المثال الأول، أو اسم آخر الفئة في سلسلة كما في المثال الثاني.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top