Some weeks ago I started practicing OO PHP and I made some classes but I don't know if I correctly understand the concepts. Because of this, I want to show some classes and I'll be very happy if someone can tell me their opinion about it and can teach me something.
I'll appreciate if you can explain the reasoning behind your assessments and modifications, and remember... I'm learning :$
I have a catalog of products and we can have multiple types of catalogs. Basically a Catalog is a collection of instances of Product class...
I have implemented a Base class for the Catalog like this:
class BaseCatalog
{
protected $_collection;
protected $_count;
public function __construct()
{
// some stuff here
}
public function getCount()
{
return $this->_count;
}
public function getProducts()
{
return $this->_collection;
}
}
and later when I would use a catalog I extend this class
class OrganicCatalog extends BaseCatalog implements InterfaceCatalog
{
protected $_collection;
protected $_count;
public function __construct()
{
parent::__construct();
// some more things here depending catalog type
}
// some more methods specific of this catalog type
And finally one interface:
interface InterfaceCatalog
{
public function getCount();
public function getProducts();
}
Some doubts about the code
- All catalog types will have same properties, the difference between them is the process to get this info.
- Interface will have methods that all catalog types will implement but only the prototype of the methods (if I would define some behavior i should use abstract classes) Is it correct?
- I think about the Base product will never be instantiated directly, I was thinking in Abstract but I've been reading that abstracts can't modify properties... maybe can i put construct to private?
- Which is the right way to declare properties (collection, count...)? Only in base? Can somebody explain it please?
- It's correct to put accessors (getters and setters) in Base class?
I think that's all... Thank you everyone for advance, i know it's a little boring to solve beginners doubts like mine.
And sorry about my English :$