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 asses开发者_如何学Pythonsments 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 :$
A couple of things:
BaseCatalog
defines all of the functions used byInterfaceCatalog
. I'd consider makingBaseCatalog
implementInterfaceCatalog
; every subclass of it will by definition be anInterfaceCatalog
as well.OrganicCatalog
should probably not have its own$_count
and$_collection
, since those are protected in the base class (and thus available to subclasses).
Abstract classes can modify properties just fine, as long as the properties are defined in the abstract class. Ideally, an abstract base class should have a nearly-complete implementation of the functionality you want subclasses to inherit (that is, "complete" minus the stuff you require subclasses to implement). Stuff that must be implemented by subclasses should be marked abstract.
If you expect all catalogs to extend BaseCatalog
, then there's no pressing need for the interface at all. Its main use would be if you expect there to be a catalog that manages everything itself (ie: it has custom getCount
and getProducts
methods). A class that does that should implement the interface but not extend the base class. If an implementation of InterfaceCatalog
can't always act like a real catalog, then perhaps the interface should be added to.
Looks pretty good so far SergiGP.
All catalog types will have same properties, the difference between them is the process to get this info.
Sounds good.
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 ?
Yes, that is correct.
I think about the Base product will never be instantiated directly, I was thinkink in Abstract but i 've been reading that abstracts can't modify properties... maybe can i put construct to private ?
This is not correct. An abstract class can perfectly well modify properties. So you surely can define BaseCatalog
as abstract. And, as cHao already mentioned, I would indeed also suggest you have BaseCatalog
implement InterfaceCatalog
.
Which's the right way to declare properties (collection, count...) ? Only in base ? Can somebody explain it please ?
and
It's correct to put accessors (getters and setters) in Base class ?
This is really dependent on the requirements you foresee for derivatives of the base class. If you think all derivatives will need the properties, and could use some base method implementations for them as well (the getters), this is perfectly fine. I don't see anything wrong with this at all.
And if you don't even want derivatives to override the methods, you can declare them as final
.
精彩评论