This is more of a best practice question than anything.
Let's say I have a form:
<form action="self.php" method="POST">
<input type="text" name="aA" />
<input type="text" name="aB" />
<input type="text" name="aC" />
</form>
And a corresponding class in PHP:
class A
{
public function getA(){
return $this->a;
}
public function setA($a){
$this->a = $a;
}
public function getB(){
return $this->b;
}
public function setB($b){
$this->b = $b;
}
public function getC(){
return $this->c;
}
public function setC($c){
$this->c = $c;
}
private $a;
private $b;
private $c;
}
And somehow I manage to send the data from the form. Now, I need the form data transformed into an instance of A.
What I'm currently doing is the following:
abstract class AStrategy {
public function doMapping(){
$a = new A();
if (isset($_POST['aA']) === true) {
$a->setA($_POST['aA']);
}
... (same for b and c)
return $a; //A completely mapped object
}
}
And I'm extremely aware this is pretty much bad practice (violating DRY).
- What's the best practice to do this?
- What if I have a complex object tree? What if I need to map related objects at the same time?
- Who should do the mapping? Who should create the object and such?
Thank you beforehand.