Is it better to have only one class color
and include the functions from the second class in the 1st one? Or leave them apart?
Why is one better than the other or vice versa? The design pattern is important to me, so why choose one over the other?
Is there a problem if, let's say, we have the function create_color
in which we instantiate the class itself like new color()
?
class color
{
private $id = NULL;
private $name = '';
private $rgb = NULL;
private $cmy = NULL;
private $wavelength = NULL;
private $frequency = NULL;
public function __construct($name, $rgb, $cmy, $wavelenght, $frequency)
{
setName($name);
setRGB($rgb);
setCMY($cmy);
setWavelength($wavelength);
setFrequency($frequency);
}
public function __destruct()
{
}
public function setName($name)
{
$this->name=$name;
}
public function setRGB($rgb)
{
$this->rgb=$rgb;
}
public function setCMY($cmy)
{
$this->cmy=$cmy;
}
public function setWavelength($wavelength)
{
$this->wavelength=$wavelength;
}
public function setFrequency($frequency)
{
$this->frequency=$frequency;
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getRGB()
{
return $this->rgb;
}
public function getCMY()
{
return $this->cmy;
}
public function getWavelength()
{
return $this->wavelength;
}
public function getFrequency()
{
return $this->frequency;
}
public function toJSON()
{
return "{'id':'".$this->id."', 'name':'".$this->name."', 'rgb':'".$this->rgb."', 'cmy':'".$this->cmy."', 'wavelength':'".$this->wavelength."', 'frequency':'".$this->frequency."'}";
}
public function toCSV()
{
return $this->id . ", " . $this->name . ", " . $this->rgb . ", " . $this->cmy . ", " . $this->wavelength . ", " . $this->frequency;
}
public function toHTML()
{
return "<p>ID: " . $this->id . "</p><p>Name: " . $this->name . "</p><p>RGB: " . $this->rgb . "</p><p>CMY: " . $this->cmy . "</p><p>Wavelength: " . $this->wavelength . "</p><p>Frequency: " . $this->frequency . "</p>";
}
}
Second class:
class CRUD_color
{
public function create_color($parameters)
{
$color=new color();
$color->setName($parameter['name']);
$color->setRGB($parameter['rgb']);
$color->setCMY($parameter['cmy']);
$color->setWavelength($parameter['wavelength']);
$color->setFrequency($parameter['frequency']);
$entitymanager->persist($color);
$entitymanager->flush();
}
public function request_color($parameters)
{
$color=$entitymanager->find($parameter['id']);
echo $color->toJSON($parameter['name']);
}
public function update_color($parameters)
{
$color=$entitymanager->find($parameter['id']);
$color->setName($parameter['name']);
$color->setRGB($parameter['rgb']);
$color->setCMY($parameter['cmy']);
$color->setWavelength($parameter['wavelength']);
$color->setFrequency($parameter['frequency']);
$entitymanager->persist($color);
$entitymanager->flush();
}
public function delete_color($parameters)
{
$color=$entitymanager->delete($parameter['id']);
}
}