PHP classes OOP store templates

I have doubts about the design of a system for managing store items using OOP. I created an abstract Product class that will have the attributes $ name and $ price. Later I created a Smartphone class that extends Product. The smartphone has the attributes $ brand and $ os. Later I created a Computer class that extends Product and has the attributes $ cpuFrequency and $ ram. Now, if I want to create an object that matches Nexus 5, for example, what do I need to do? Consider, for example, what this object will have

$name = "Nexus 5" $price = "250" $brand = "LG" $os="Android" $cpuFrequency "2.0" $ram = "2". 

Here is an example of classes.

 abstract class Product { private $name; private $price; //more methods } class Smartphone extends Product{ private $brand; private $os; } class Computer extends Product { private $cpuFrequency; private $ram; } 

It is also believed that there can be a computer without $ os and a smartphone without $ ram. just one example: D

PS I know that a smartphone is a computer. I made a bad example. Please think on the phone and computer. The phone has $ number and $ operator. Now I want to create a smartphone that is both a telephone and a computer.

Thanks everyone and sorry again for my English

+6
source share
5 answers

You can try using the ProductProperties class, for example:

 class ProductProperties { private properties = array(); public function getProperties() { return this->properties; } public function addProperty($id, $value) { this->properties[$id] = $value; } public function getProperty($id) { return this->properties[$id]; } } abstract class Product { private $properties; //more methods public function __construct(ProductProperties $properties) { $this->properties = $properties; } } 

You can pass an object of the ProductProperties class to any of your products and let it handle all the problems associated with it. This would create a more flexible design, so you do not need subclasses just because the product has a different set of properties. Remember that subclassification is a change or addition of behavior.

+1
source

Typically, you should write constructors for your classes in some way that the "child" class initializes the superclass so that everything is consistent.

 abstract class Product { private $name; private $price; //more methods public __construct($name, $price){ $this->name = $name; $this->price =$price; } } class Smartphone extends Product{ private $brand; private $os; // default value to $os: like that, we can create a Smartphone whitout an os public __construct($name, $price, $brand, $os = null){ parent::__construct($name, $price); $this->brand = $brand; $this->os =$os; } } class Computer extends Product { private $cpuFrequency; private $ram; // default value to $ram: with this, we can create a Computer whiout ram public __construct($name, $price, $frequency, $ram = null){ parent::__construct($name, $price); $this->frequency = $frequency; $this->ram =$ram; } } 

Then, to create a computer, you can do, for

 $computer = new Computer('notBuiltToWindows', 560 , 2.0, 4); 

or, to create a Nexus in your example:

 $nexus = new Smartphone($name, $price, $frequency, $ram); 

and to create a smartphone without a ram

 $nexus = new Smartphone($name, $price, $frequency); 
+2
source

As others have said, too many classes can get confused.

The idea is to create a class that can span many different scenarios, without much effort.

For example, you might have a class Pet . A dog is a pet, and therefore it can be a snake ... This does not mean that you must make a FourLegged or Nolegs .

Like your products, you can have a product class that contains things common to all products. You might even want to have a subclass of the electronics class that can span anything from phones to computers.

You can have a productType attribute that says what kind of electronic it is (for example, a phone), but in the end there will not be too many differences between individual electronics: televisions, smartphones and computers have screen specifications, a smartphone and a computer have RAM, CPU, etc. .d.

0
source

How we do this at work is a combination of most of the answers above:

1) You have a product class / object - this contains common things that any product will have - name, description, some images, etc.

2) You have a subclass that contains product attributes (product attribute class) - for example. size, weight (not all products are physical, therefore not all have dimensions or weight), price, number_of_ram_slots, cupFrequency.

We allow you to define attributes using ORM, which maps the attributes to the database. This is becoming the best of both worlds due to the fact that php cannot do multiple inheritance - technically, a smartphone is both a computer and a phone, but doing it above does not matter, because each of them is just a product and a set custom attributes.

Essentially this is the same answer as @Edgar implementation

0
source

At the top level, we create concepts, intangible things. Things that are in theory and must be realized in order to be tangible.

 // make product an interface, or in other words a concept. its not tangible, // but instead its just a concept thus making it an interface interface Product { public function getName(); public function getPrice(); public function getBrand(); } // A computer is also a concept, thats implemented by many devices like a laptop, // SMARTPHONE, desktop, calculator, etc... interface Computer { public function getRam(); public function getFrequency(); public function getOS(); } 

Phone Logic:

 // We can implement the concept of a Product here // the phone is also a concept, implemented by home phones, // telephones and mobile phones so we make it abstract abstract class Phone implements Product { private $name; private $price; private $brand; function __construct ($name, $price, $brand) { $this->name = $name; $this->price = $price; $this->brand = $brand; } public function getName() { return $this->name; } public function getPrice() { return $this->price; } public function getBrand() { return $this->brand; } } 

Smartphone Logic:

 // Smartphone is another concept implemented by many devices like a Nexus 5, // what makes this concept different from a Phone is that it doubles as a computer. // so we extend the concept of a phone by implement the concept of a computer // making the Phone 'smart' abstract class Smartphone extends Phone implements Computer { private $os; private $frequency; private $ram; public function __construct ($name, $price, $brand, $os, $frequency, $ram) { parent::__construct($name, $price, $brand); $this->os = $os; $this->frequency = $frequency; $this->ram = $ram; } public function getRam() { return $this->ram; } public function getFrequency() { return $this->frequency; } public function getOS() { return $this->os; } } 

Finally, we have a complete Concept, something doable / tangible. Nexus Product.

 // A Nexus implements the concept of a Smartphone. // so now the Nexus is a Smartphone, Computer, Phone and a Product class Nexus extends Smartphone { public function __construct ($version) { parent::__construct('Nexus '.$version, 250, 'LG', 'Android', '2.0', 2); } } 

Using:

  // all we do is specify the version of nexus we are using $nexus5 = new Nexus(5); 
0
source

Source: https://habr.com/ru/post/979355/


All Articles