I have a doubt about the design of a system to manage items of a store using OOP. I created the abstract class Product that will have the attributes $name and $price. Later I created the class Smartphone that extends Product. Smartphone has the attributes $brand and $os. Later I created the class Computer that extends Product and has attributes $cpuFrequency and $ram. Now, if I want to create an object corresponding to the Nexus 5, for example, what i have to do? Consider, for example, that this object will have
$name = "Nexus 5"
$price = "250"
$brand = "LG"
$os="Android"
$cpuFrequency "2.0"
$ram = "2".
Here 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;
}
Also considered that there may be Computer without $os and Smartphone without $ram. just one example: D
P.S. I know that smartphone is a computer. I did a bad example. Please think at Telephone and Computer. Telephone has $number and $operator. Now i want to create a Smartphone that is at the same time Telephone and Computer.
Thanks for all and sorry again for my english
A class is a template for objects, and an object is an instance of class.
PHP What is OOP? OOP stands for Object-Oriented Programming. Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions.
Polymorphism is essentially an OOP pattern that enables numerous classes with different functionalities to execute or share a commonInterface. The usefulness of polymorphism is code written in different classes doesn't have any effect which class it belongs because they are used in the same way.
In Object Oriented Programming in PHP, methods are functions inside classes. Their declaration and behavior are almost similar to normal functions, except their special uses inside the class.
Normally, you should write constructors to your classes in some way that "child" class initialize the super class, to keep everything 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 the Nexus in your example:
$nexus = new Smartphone($name, $price, $frequency, $ram);
and, to create a smartphone whitout a ram
$nexus = new Smartphone($name, $price, $frequency);
You could try using a ProductProperties class like so:
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 could pass an object of class ProductProperties to any of your product and let it handle all properties related issues. This would make for a more flexible design, this way you don't need to subclass just because a product has a different set of properties. Remember subclassing is all about changing or adding behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With