bernardnthiwa Posted February 22, 2023 Share Posted February 22, 2023 Hello, I'm configuring php api endpoints for crud operations with a React.JS frontend. I would like help on how to implement the logic with OOP, an abstract class for the main product logic. I have three product types, Book, Furniture, and DVD. All products have a name, sku (primary key), and price. Moreover, each product type has a product-specific value. A book has a weight property, furniture has dimensions (length, width, and height) and a DVD has a size property. How can I proceed to use polymorphism to achieve this, so that i can avoid using conditional statements to detect product type. Quote Link to comment https://forums.phpfreaks.com/topic/315937-implementing-polymorphism-and-inheritance/ Share on other sites More sharing options...
requinix Posted February 22, 2023 Share Posted February 22, 2023 You can't. There will have to be some conditional statement somewhere. OOP is a technique to solve a problem. It is not a solution to a problem. What's the rest of your design, and we can see if OOP is going to help with this particular task. Quote Link to comment https://forums.phpfreaks.com/topic/315937-implementing-polymorphism-and-inheritance/#findComment-1605903 Share on other sites More sharing options...
gizmola Posted February 25, 2023 Share Posted February 25, 2023 On 2/22/2023 at 9:56 AM, bernardnthiwa said: Hello, I'm configuring php api endpoints for crud operations with a React.JS frontend. I would like help on how to implement the logic with OOP, an abstract class for the main product logic. I have three product types, Book, Furniture, and DVD. All products have a name, sku (primary key), and price. Moreover, each product type has a product-specific value. A book has a weight property, furniture has dimensions (length, width, and height) and a DVD has a size property. How can I proceed to use polymorphism to achieve this, so that i can avoid using conditional statements to detect product type. I assume you have a database behind this. How have you solved it with database design? Typically this would be "subtyping". So polymorphism is not involved. What is involved would be inheritance. But I'd start with your assumption here. You basically have a list of "Finished Goods". All of them have dimensions and weight. Is this academic, because your original supposition is arbitrary and incorrect? Rather, it seems that you want to associate one or more product categories or tags to the finished goods in this list. Let's assume you had a product_type table that included a primary key and a name. That table would have in it: 'Book', 'DVD', and 'Furniture' and likely many more. You'd classify the product by having a foreign key for this table in the product table. Now beyond this, there could be any of a million different facts that relate to a product. How do you think an ERP or Amazon solves this problem? Do you think they formulate a bunch of structure specifically for each type before it can be sold? One answer is: use properties. In the database consider how you might allow for One-to-many properties such that they could be assigned to a product. When you need something generic and extensible, you often have to design something generic and extensible. Another answer is to use a structure that is intrinsically hierarchical, like json, and allow each product to store data in json format. This can then alleviate the problem with the rigidity of relational databases, in cases where you might need some data that isn't always required, and also isn't always structured exactly the same way. Some databases have json types, and then there are "Document" databases like Mongodb, which is built upon a version of json, and essentially uses json as its internal structure. From document to document in a Mongodb collection, the json structure can vary. So you could have a basic structure, and then a section that completely varies by product type. Quote Link to comment https://forums.phpfreaks.com/topic/315937-implementing-polymorphism-and-inheritance/#findComment-1605991 Share on other sites More sharing options...
NotionCommotion Posted February 25, 2023 Share Posted February 25, 2023 Super/sub type DB modelling provides a more structured approach for inheritance and centralizes the conditional statement logic and IMO provides polymorphism, however, be careful as if taken too far, will likely paint you into a corner that you didn't want to be in, so use sparingly when the benefits outweigh the risks. Quote Link to comment https://forums.phpfreaks.com/topic/315937-implementing-polymorphism-and-inheritance/#findComment-1605998 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.