0

I've implemented an abstract class in PHP which manages all my database connections. This class has all basic methods I need like writing, reading, deleting...

To use this class now, I'm creating a special class for each database table:

class Data_Store_Snacks extends Data_Store_Abstract {

    public function __construct() {
        $this->table       = Install::SNACKS_TABLE_NAME;
        $this->primary_key = 'id';
        $this->model       = Data_Store_Snacks_Model::class;

        parent::__construct();
    }
}

In the above class I'm setting the table, primary key and model which should be returned. The problem is that in the abstract class above this class, I have this method for example:

/**
 * Retrieves multiple table rows as an array, filtered by the where
 *
 * @param array $where
 * @param array|null $order_by
 * @param int|null $limit
 * @param int|null $offset
 *
 * @return array|false
 */
public function find_all_by( array $where, array $order_by = null, int $limit = null, int $offset = null ) {

Currently, it's returning an array or false. Instead of the array, I want to return the model as an array like:

@return Data_Store_Snacks_Model[]|false

This works, but not for more than one table! Instead, I need something dynamic like:

@return $this->model[]|false

But as far as I know this is not possible. Currently, I'm using a @var definition within a foreach but I don't like doing this to just have my methods available in my IDE

Any ideas?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Mr. Jo
  • 4,946
  • 6
  • 41
  • 100
  • Does this answer your question? [PHPDoc type hinting for array of objects?](https://stackoverflow.com/questions/778564/phpdoc-type-hinting-for-array-of-objects) – Peter Krebs Nov 18 '21 at 11:39
  • @PeterKrebs No, it don't since they are all working with static models. I'm talking about dynamic hinting.. – Mr. Jo Nov 18 '21 at 11:46
  • Can you use [`@method`](https://stackoverflow.com/a/20992124/231316) on the individual classes? I know it isn’t dynamic, but it also shouldn’t be too hard to throw it on your individual classes just once. – Chris Haas Nov 18 '21 at 12:17
  • @ChrisHaas thats what I'm currently doing but several times. I was hoping that there is a way to return the model saved to the class variable as type... – Mr. Jo Nov 18 '21 at 13:41
  • 1
    My gut says no, which is obviously not an authoritative answer. Your use-case is totally valid, but I don't think there's a way describe it both accurately and generically. – Chris Haas Nov 18 '21 at 14:15

1 Answers1

1

Not how you describe, but you could add an interface to your models like so:

interface Model_Contract{}

class Data_Store_Snacks_Model implements Model_Contract {}
class Data_Store_Drinks_Model implements Model_Contract {}

/**
 * ...
 * @return Model_Contract[]|false
 */
Ron van der Heijden
  • 14,803
  • 7
  • 58
  • 82