Hi Alex, you should check PHP Interfaces
Nov 2, 2022
4
Level 1
Which design pattern is this, if any?
Hello there, I was wondering if someone can help me by identifying this pattern (if this is even a design pattern to begin with). I've got the following class:
class myGeneralClass
{
/**
* @var Model { Class1, Class2 }
*/
private Model $model;
public function __construct(Model $model){
$this->model = $model;
}
public function getData() : mixed {
return $this->model::first();
}
public function setData(array $data) : void{
$this->model::create($data)
}
}
Which I would invoke like this:
$main = new myGeneralClass(new Class1);
$main->getData(); // some data from that model
$main->saveData($data) // save some data
I've got several methods which do exactly the same thing, and I just swap the model in/out whenever and wherever I need to. I was wondering if there is a specific pattern related to this kind of code, and if there is I'd love to learn / read more about it and check all the best practices.
Thanks in Advance
Alex
Please or to participate in this conversation.