5

I'm trying to learn OOP in PHP, for now i have reached the abstract classes.

I have some problems understanding when should I implement abstract methods in the abstract class and when I should not.

For example, have this code:

<?php

abstract class concept_car {
    /**
     * Properties can not be declared as abstract natively:
     * http://stackoverflow.com/questions/7634970/php-abstract-properties
     */
    protected $weelNum  = NULL;
    protected $doorsNum = NULL;
    protected $color    = NULL;
    protected $carType  = NULL;
    // the inheritance class must define this methods
    abstract public function setWeelNum($weelNum);

    abstract public function setDoorsNum($doorsNum);

    abstract public function setCarType($carType);
}

I do not know if it is OK to declare the 3 methods as abstract or should I remove the abstract and implement them because the properties are in the same class as the methods.

In the actual form of the code I was thinking that I must declare the methods as abstract here and implement them in the inheritance class, in the child class, but I don't know if this is the correct way.

P.S: I am a beginner and I am trying to understand how things go, I didn't reach at design patterns yet so please explain me in concepts so i can know what is the right way to proceed.

4
  • 2
    Actually, they are simply setters. I think you should implement them now. Commented Jun 4, 2013 at 12:49
  • I kindly recommend thinking about classes and objects as an analogy to thier real life counter-parts, ask the same questions for the real thing and try to answer it, you'll most likely end up with a good design. Also try to think how design decisions will affect your code base down the road, for example here if you don't implement those set*() methods you'll have to implement them later in every car class. Commented Jun 4, 2013 at 14:09
  • Also, lose the design patterns stereotype, please. The patterns emerge from good code, you just write OOP without thinking of patterns (at least for now). Those patterns are nothing more than a set of classes that everybody knows how they behave by a common name. Commented Jun 4, 2013 at 14:11
  • @Paul Thank you for the explanations, I'll do my best in staying on the right track. Commented Jun 4, 2013 at 17:41

2 Answers 2

7

Simple question you have to ask yourself:

Do all classes that extend this one do the same thing?

For this example, the answer is yes, they will all do

public function setWeelNum($weelNum) {
  $this->weelNum = $weelNum;
}

So don't copy/paste that code in each class, it's no point.

When should you declare an abstract method ?

When all the children of that class must implement this method but in different ways.

For example, in the class Animal, you will have a public abstract function move(); because all animals move, but you don't know how exactly.

Sign up to request clarification or add additional context in comments.

7 Comments

When all the children of that class must implement this method but in different ways. I'd say that is a case for an interface, not an abstract class.
@crush I totally agree with you.
I agree too, no one said that an abstract class didn't implement that interface.
@Starlays In (very) short: use interfaces in classes that do not have a common ancestor. Use abstract class if they do (this abstract class)
You should use an interface when you are only describing an interface...that's why it's called an interface. An abstract class should really have some common members, like your example above has, @Starlays.
|
4

Generally, you should implement all methods in the last common ancestor. So if you want your children classes will behave the same way, implement them now. If they are to be implemented, but in many ways by different classes, leave them abstract.

Actually, an abstract class is done only for requiring that if somebody will inherit something from this common class, he will provide these methods (abstract class is something similar to interface).

If you implement all of them, there is no need to use an abstract class.

2 Comments

They are "default" setters for the properties of that base class. I think they should be implemented in that class. If every base class is going to implement them differently, then this should be an interface, and not an abstract class.
@crush Yes, I missed that, in this way it's hard to imagine they could do something else (notify observers?). I think Oltarus's answer explains it better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.