使用多态性而不是有条件的语句PHP
我正在PHP中练习OOP概念。我正在开发一个网站,将产品添加到MySQL数据库中。
每个产品都有其product_id,姓名,价格和product_type。
应插入3种产品类型(笔记本电脑,电视和扬声器)。每种类型的产品都有不同的属性。
例如,
Product id : 1 Name : DELL N5110 Price : $2000 Product type : Laptop CPU : Intel Core i5 RAM : 8GB DDR4 SSD : 512GB Product id : 2 Name : Samsung NU7100 Price : $900 Product type : TV Size : 42 Inches Product id : 3 Name : Creative SB1000 Price : $90 Product Type : Speakers Power : 300w Channels : 2.1
我需要为每种产品创建单个类文件,并使用多态性插入数据,而不是使用条件语句来处理产品类型的差异。
不要为不同产品类型使用不同的端点。产品节省应该有1个总终点
I am practicing OOP concept in PHP. I’m developing a site to add products into mysql database.
Every product has its product_id, name, price and product_type.
3 types of products should be inserted (Laptop, TV and Speakers). And each type of products has different attributes.
Example,
Product id : 1 Name : DELL N5110 Price : $2000 Product type : Laptop CPU : Intel Core i5 RAM : 8GB DDR4 SSD : 512GB Product id : 2 Name : Samsung NU7100 Price : $900 Product type : TV Size : 42 Inches Product id : 3 Name : Creative SB1000 Price : $90 Product Type : Speakers Power : 300w Channels : 2.1
I need to create individual class files for each product and insert data using Polymorphism instead of using conditional statements for handling differences in product types.
Do not use different endpoints for different products types. There should be 1 general endpoint for product saving
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以做下面的事情。 “产品”类(父)具有所有“基础”属性和方法。在“儿童”类中,您可以为每个产品类型提供额外的方法和属性。
You can do something like below. The 'Product' class (parent) has all the 'base' properties and methods. In the 'Child' class you can extend with extra methods and properties for each producttype.