2

I have two classes Class A and Class B

In Class A, I have three methods that perform the save, delete and select operation based upon the object I pass them. In Class B I perform the logic operations, such as modification to the property of the object before being passed to the methods of Class A.

My problem is in Class B, should it extend Class A, and call the methods of class A , by parent::methodName or create instance of class A and then call

Class A does not includes any property just methods.

class A{
  public function save($obj){
         //code here
    }

   public function delete($obj){
         //code here
    }

    public function select($obj){
         //code here
    }
 }

//Should I extend class A, and call the method by parent::methodName($obj) or create an instance of class A, call the method $instanceOfA->methodName($obj);

 class B extends A{

    public function checkIfHasSaved($obj){
         if($obj->saved == 'Yes'){
                  parent::save($obj);   //**should I call the method like this**
                 $instanceOFA = new A();   //**or create instance of class A and call without extending class A** 
                 instanceOFA->save($obj);
            }
             //other logic operations here
      }

 }

3 Answers 3

3

The examples given by you could have had meaningful class names, this would favor much better responses.

Since class A does not have any properties, it is safe to use methods from the instance you already have:

class B extends A
{
    public function checkIfHasSaved($obj)
    {
        if($obj->saved)
            $this->save($obj);
    }
}

Another alternative would be to use Dependency injection instead:

class B
{
    protected $a;

    public function __construct(A $theObject)
    {
        $this->a = $theObject;
    }

    public function checkIfHasSaved($obj)
    {
        if($obj->saved)
            $this->a->save($obj);
    }
}
2

Without knowing the names and purposes of the real classes it's hard to say. The decision hinges on whether class B is really a type of class A or whether class B just needs some service class A provides. For example, a lot of programmers make the mistake of building model classes by extending a database access layer class on the grounds that they need to access the database in order to persist their state. This is almost always a mistake. A model class needs a database connection but it almost certainly isn't a kind of database connection.

It boils down to whether the link between the classes A and B is an is-a relationship or a has-a relationship. In the former it makes sense to think of class B as a kind of class A, and if it's impossible for class B to gain or lose the behaviour of class A during its lifetime then an is-a relationship (subclassing) makes sense. If the behaviour of class A is something that class B only needs under some circumstances, or if it doesn't make sense to think of class B in terms of being a kind of class A then composition (has-a relationship) is the more sensible approach.

Say you're modelling a school and have Person, Student and Teacher classes. At first glance you might think that Student and Teacher are kinds of Person, and therefore Student and Teacher should extend Person. But what happens should a particular student get a job at the school as a teacher after they graduate? Student and Teacher are in fact roles that a Person can take on, they can discard those roles at the right time too. In some systems the same person could hold more than one role at once (suppose a doctor gets hit by a car when going to work in a hospital, she could end up as a patient at that hospital!)

On the other hand, if you have a Car class, a Ferrari class and a Bugatti class, then it makes sense for Bugatti and Ferrari to be subclassed from Car. The reason is that Ferraris and Bugattis are both kinds of car, and a Ferrari is always going to be a Ferrari from the moment it leaves the factory to the moment it's scrapped. There's no circumstance under which a Ferrari is going to become a Bugatti.

So basically, TL:DR, it depends.

1

Since it's semantically correct to ask your object if it's saved then I guess it would be OK to ask it to save. If you extend saving will be a public method on your object and available from the outside.

You'll need to decide if that's OK or not.

2
  • the logic i did is an example in class B, it involves much more logic operations in class B before calling class A method Nov 26, 2012 at 10:22
  • the point still stands, if it's ok for B to save then extend it from A, if not use an instance in your particular method. For me though, without seeing more of your code, I would expect the ability to save if I could check if I had saved. Nov 26, 2012 at 10:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.