Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP OOP classes of store pattern

Tags:

oop

php

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

like image 580
GeeGee Avatar asked Dec 09 '14 19:12

GeeGee


People also ask

What is class in OOP in PHP?

A class is a template for objects, and an object is an instance of class.

What is OOP in PHP with example?

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.

What is polymorphism PHP?

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.

What is method in OOP PHP?

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.


2 Answers

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);
like image 65
Hilder Vitor Lima Pereira Avatar answered Nov 05 '22 02:11

Hilder Vitor Lima Pereira


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.

like image 24
Edgar Avatar answered Nov 05 '22 04:11

Edgar