I am a bit new to PHP OOP so I need you guys help here. I am trying to understand when to use abstract function inside abstract class.
for example
abstract class User{
abstract function login();
//how do I decide if I should make login function as abstract or regular function.
abstract function permission();
function AccountCredit (){
echo 'show account credits';
}
}
An abstract class is a class that doesn’t ever make sense to actually create (instantiate), like if your classes were “Beagle” and “Terrier”, a “Dog” doesn’t really make sense.
Color and hairtype would only apply to “Beagle” and “Terrier” but not “Dog”, right? So it would be abstract at the Dog level.
Any method that only applied to a single breed would just not exist in “Dog” at all.
numberOfLegs would have an implementation at “Dog” level (not abstract) and the children could just inherit it (by not saying anything about it).
Note that if you were instantiating actual dogs, the breeds will become abstract–also if you were instantiating actual dogs they might need to overried numberOfLegs to account for individual dogs that have had accidents.
If you are new to OO, however, may I make a suggestion? Keep all inheritence to a minimum, only do it when you are really forced to. It seems that when a good programmer “Gets” OO their first thought is to link everything into one giant object tree–this turns out not to work very well. You are better off using encapsulation instead of extending nearly every time.