Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 102499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:58:31+00:00 2026-05-11T00:58:31+00:00

I have the current basic structure for each domain object that I need to

  • 0

I have the current basic structure for each domain object that I need to create:

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;     } } 

I initially tried to refactor the static methods into the parent LP_Model class only to learn finally what ‘late static binding’ means in the php world.

I’m just wondering if anyone has suggestions on how to refactor this code so that I don’t have to redeclare the same three functions in every domain object that I create?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:58:31+00:00Added an answer on May 11, 2026 at 12:58 am

    How about this:

    <?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';  ?> 

    Unfortunately, to make the functions easy to call, you have to duplicate get() and getCollection() in each subclass. The other option is to call the function in the parent class:

    $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'; 

    You can rename the base class and its function names if you want to go that route. But the point is that you must name the child class in one place or the other: either make a boilerplate function in the child class as in my first example, or else name the class in a string as in my second example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You are trying to bind to an item which does… May 11, 2026 at 11:41 am
  • added an answer Your code was almost correct. You just need to remove… May 11, 2026 at 11:41 am
  • added an answer I would use a Java API that wraps libpcap. libpcap… May 11, 2026 at 11:41 am

Related Questions

I have the current basic structure for each domain object that I need to
What options do I have to read the roles of the current user from
I have the following before_filter : def find_current_membership respond_to do |wants| wants.html { @current_membership
I have an application that launches a webpage in the current browser when the
I have a web application that uses the current version of JQuery that needs
I have been using PRETTY_FUNCTION to output the current function name, however I have
I have an app that display's the current time when a page opens. I
I have become accustomed to using F6 to compile the current document. A third-party
I have a bash script that creates a Subversion patch file for the current
I have a shell script which copies a few files to the current directory,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.