Standard way for having common operations in a one class and use them by other classes in PHP [closed]

I am having multiple inventory classes Inventory.php, ReturnInventory.php, IssueInvetory.php etc.

I have common operations in all these classes like isItemAlreadyApproved(), isItemAlreadyRollback(), isItemHasEnoughQty(), etc.

I thought to have an interface for these operations and implements it in all the above inventory classes so that I can use those operations without writing the same there?

Or having these operations in a base class and extends it in all the above inventory classes so that I can use those operations without writing the same there?

Or having a separate class for these operations and use them in the controllers or in any inventory related class?

I am not sure 100% what are the industry standards and best practices doing this.

Could someone suggests a good way to achieve this?

  • Yes, these are pretty much your options. I’ll add traits as another code-reuse mechanism. Which one is the most appropriate for your situation we cannot tell you.

    – 

  • @deceze as i read in other questions, traits are like simply a “copy” and “paste” for the parent class. I tried, but need to use a construct in trait so that i can share the same DB connection among functions. for that, i cant use trait. also if i use as a base class, i can’t use the same db connection when i use static methods in the sub class.

    – 




  • as a temporary solution i use it as a separate class and every time i need it, i use it as a new object.

    – 

Leave a Comment