Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You might try using interfaces instead.</p> <pre><code>interface OtherModel { public function getThis(); } interface OtherOtherModel { public function getThat(); } </code></pre> <p>Your Model Class might implement one or more interfaces...</p> <pre><code>class PersonModel extends Model implements OtherModel { protected $_fields = array('id', 'name', 'passhash', /*...*/); public function setId($value) { /* ...Validation happening... */ $this-&gt;_data['id'] = $value; return $this; } public function checkPassword($password) { /* ...magic... */ } public function getThis() { // ... } } </code></pre> <p>Your concrete Mapper Class can use the instanceof to check if this Model does what it should.</p> <pre><code>class PersonMapper extends DataMapper { public function findById($id) { /* ...magic... */ } public function fetchAll() { /* ...magic... */ } public function save(Model $model) { // verify that certain methods are implemented... // throw an exception or reacting accordingly print ($model instanceof PersonModel)? 'yes' : 'no'; print ($model instanceof OtherOtherModel)? 'yes' : 'no'; } } </code></pre> <p>Another possible approach might be the following:</p> <pre><code>&lt;?php abstract class DataMapper { public abstract function findById($id); public abstract function fetchAll(); public function save(Model $model) { throw new Exception('You have to implement this!'); } } </code></pre> <p>Throw an Exception if the save method is not overriden in an inheriting class. Now you can really use a different typehint. This will work:</p> <pre><code>class PersonMapper extends DataMapper { public function findById($id) { /* ...magic... */ } public function fetchAll() { /* ...magic... */ } public function save(PersonModel $model) { // do something } } </code></pre> <p>I could think of another possible approach, by using interfaces to define the implementation. Like for example:</p> <pre><code>interface PersonModelAware { public function save(PersonModel $model); } interface OtherModelAware { public function save(OtherModel $model); } </code></pre> <p>etc. Your abstract method might have a default save method or no save method at all. The inheriting class will implement the interface it needs. </p> <p>To sum it up, making your type more specific will not work as the abstract method clearly states it expects a Model. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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