Note that there are some explanatory texts on larger screens.

plurals
  1. POHow Many Static Methods is Too Many For One Class?
    text
    copied!<p><b>UPDATE:</b> Rephrasing the question to ask, 'are there too many' static methods (I realize that right now there are only 4 but I originally started with 2) in this class structure? If so, any suggestions on how to refactor these classes to use some sort of Finder class so that I can remove the static functions from the Model classes?</p> <p>I have the following abstract class:</p> <pre><code>abstract class LP_Model_Abstract { protected static $_collectionClass = 'LP_Model_Collection'; protected $_row = null; protected $_data = array(); public function __construct($row = null) { $this-&gt;_row = $row; } public function __get($key) { if(method_exists($this, '_get' . ucfirst($key))) { $method = '_get' . ucfirst($key); return $this-&gt;$method(); } elseif(isset($this-&gt;_row-&gt;$key)) { return $this-&gt;_row-&gt;$key; } else { foreach($this-&gt;_data as $gateway) { if(isset($gateway-&gt;$key)) { return $gateway-&gt;$key; } } } } public function __set($key, $val) { if(method_exists($this, '_set' . ucfirst($key))) { $method = '_set' . ucfirst($key); return $this-&gt;$method($val); } elseif(isset($this-&gt;_row-&gt;$key)) { $this-&gt;_row-&gt;$key = $val; return $this-&gt;_row-&gt;$key; } else { foreach($this-&gt;_data as $gateway) { if(isset($this-&gt;_data[$gateway]-&gt;$key)) { $this-&gt;_data[$gateway]-&gt;$key = $val; return $this-&gt;_data[$gateway]-&gt;$key; } } } } public function __isset($key) { return isset($this-&gt;_row-&gt;$key); } public function save() { $this-&gt;_row-&gt;save(); } abstract public static function get($params); abstract public static function getCollection($params = null); abstract public static function create($params); </code></pre> <p>}</p> <p>And then this class which provides additional functionality for class table inheritance schemes (where type is important in determining additional functionality in a factory fashion):</p> <pre><code>abstract class LP_Model_Factory_Abstract extends LP_Model_Abstract { protected static $_collectionClass = 'LP_Model_Collection_Factory'; abstract public static function factory($row); } </code></pre> <p>These ultimately result in the following type of class declaration:</p> <pre><code>class Model_Artifact extends LP_Model_Factory_Abstract { protected static $_artifactGateway = 'Model_Table_Artifact'; public static function create($params) { } public static function get($params) { $gateway = new self::$_artifactGateway(); $row = $gateway-&gt;fetchArtifact($params); return self::factory($row); } public static function getCollection($params = null) { $gateway = new self::$_artifactGateway(); $rowset = $gateway-&gt;fetchArtifacts($params); $data = array( 'data' =&gt; $rowset, 'modelClass' =&gt; __CLASS__ ); return new self::$_collectionClass($data); } public static function factory($row) { $class = 'Model_Artifact_' . $row-&gt;fileType; } } </code></pre> <p>When do you know that you have too many static methods in a class? And how would you refactor the existing design so that the static methods are perhaps encapsulated in some sort of Finder class?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload