should I extend or create instance of the class

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
      }

 }

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);
    }
}

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.

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật