tarlejh Posted September 17, 2008 Share Posted September 17, 2008 I'm still learning the OOP essentials, so please bear with me. Is it fair to say that abstract classes and interfaces are just another abstraction layer above classes? Or to put it another way, if a class is a blueprint for an object, could an abstract class be thought of as a blueprint for a class? Quote Link to comment https://forums.phpfreaks.com/topic/124708-abstract-classes-and-interfaces/ Share on other sites More sharing options...
Mchl Posted September 17, 2008 Share Posted September 17, 2008 One could look at abstract classes like that, yes. Quote Link to comment https://forums.phpfreaks.com/topic/124708-abstract-classes-and-interfaces/#findComment-644139 Share on other sites More sharing options...
DarkWater Posted September 17, 2008 Share Posted September 17, 2008 Basically, yes. All classes that implement an interface or abstract class would need to define the methods in the class or interface themselves. This means that any class can be treated the same as long as it implements or extends the interface or class, because you'll know they have the same methods. Quote Link to comment https://forums.phpfreaks.com/topic/124708-abstract-classes-and-interfaces/#findComment-644140 Share on other sites More sharing options...
tarlejh Posted September 17, 2008 Author Share Posted September 17, 2008 OK thanks...so, is it always good practice to abstract your object model down to abstract classes? Can you abstract an abstract class? Where does it end? Quote Link to comment https://forums.phpfreaks.com/topic/124708-abstract-classes-and-interfaces/#findComment-644180 Share on other sites More sharing options...
DarkWater Posted September 17, 2008 Share Posted September 17, 2008 You'd really only need to abstract your classes if you have a bunch of related classes that handle their internal business logic differently, but are called the same way. Ex: User abstract class, Member, Admin, Moderator, etc. The latter 3 can all do $user->something();, but each will do them differently. Quote Link to comment https://forums.phpfreaks.com/topic/124708-abstract-classes-and-interfaces/#findComment-644182 Share on other sites More sharing options...
aschk Posted September 19, 2008 Share Posted September 19, 2008 Abstract classes (and interfaces) are typically used to define a "pattern" for any child classes. e.g. <?php // Human - Abstract class abstract class human { // Retrieve name - must be implemented by all concrete children. abstract public function getname(); abstract public function getage(); abstract public function hurl(); } // Jane - Concrete class class jane extends human { public function getname(){ return "jane"; } public function getage(){ return 23; } public function hurl(){ echo "i can't throw i'm a girl!"; } } // John - Concrete class. class john extends human { public function getname(){ return "john"; } public function getage(){ return 27; } public function hurl(){ echo "John has throw(n) up..."; } } ?> An abstract class is used to define a concrete class. You CANNOT instantiate an abstract class (i.e. $class = new AbstractClass() ). Any concrete children MUST implement any declared abstract functions. Now when you come to use the child classes you know that they all maintain the same methods (but with different outcomes). <?php function testPerson(human $person){ $person->hurl(); } testPerson(new jane()); testPerson(new john()); ?> Because we know that jane's and john's both implement the hurl() function we can interchange them throughout our code, replacing one with the other at any time without worrying about having to modify our code for the new class type. Quote Link to comment https://forums.phpfreaks.com/topic/124708-abstract-classes-and-interfaces/#findComment-645543 Share on other sites More sharing options...
Daniel0 Posted September 21, 2008 Share Posted September 21, 2008 Actually, there is a fault in your design, aschk. Considering jane cannot hurl, then it shouldn't have that method defined. You could've used the strategy pattern to overcome this. Quote Link to comment https://forums.phpfreaks.com/topic/124708-abstract-classes-and-interfaces/#findComment-646916 Share on other sites More sharing options...
aschk Posted September 25, 2008 Share Posted September 25, 2008 I don't believe a strategy pattern would overcome this issue Dan. Normally I would consider throwing an exception, but in order to demonstrate what the OP asked for, which was a differentiation of classes into abstracts/interfaces, I believe i have covered the point well. "Program to an implementation" I'd be keen to here your strategy suggestion however, as I often believe there is inherent weakness in methods I implement that aren't "used" in all children. If you are suggesting that you replace the hurl() method with a strategy then again you are faced with essentially the same problem. I shall demonstrate: <?php // Human - Abstract class abstract class human { protected $hurlStrategy; // Retrieve name - must be implemented by all concrete children. abstract public function getname(); abstract public function getage(); public function setHurlStrategy(strategy_hurl $hurl){ $this->hurlStrategy = $hurl; } public function hurl(){ $this->hurlStrategy->perform(); } } // Jane - Concrete class class jane extends human { public function __construct(){ $this->setHurlStrategy(new strategy_hurl_girl()); } public function getname(){ return "jane"; } public function getage(){ return 23; } } // John - Concrete class. class john extends human { public function __construct(){ $this->setHurlStrategy(new strategy_hurl_boy()); } public function getname(){ return "john"; } public function getage(){ return 27; } } abstract class strategy_hurl { abstract public function perform(); } class strategy_hurl_boy { public function perform(){ echo "throw(n) up"; } } class strategy_hurl_girl { public function perform(){ echo "i can't throw i'm a girl!"; } } ?> The calling script i included above still applies. Jane still can't hurl... Quote Link to comment https://forums.phpfreaks.com/topic/124708-abstract-classes-and-interfaces/#findComment-650442 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.