I have a seamingly simple problem which I can't figure out and I don't know the name of what I'm looking for.
How do I copy data in a simple way to an extending class? Suppose I have this:
class A {
private string $a;
private string $b;
private string $c;
}
class B extends A {
private DatetimeInterface $completedAt;
}
$a = new A(1, 2, 3); // via an imaginary contruct()
I now have processed A and want to move it to another table B. I want a new B()
, but with all the values of A to start with.
// Pseudo code of what I (think I) am looking for:
$b = MagicallyConvertAToB($a);
$b->setCompletedAt(new Datetime());
B
will always be identical toA
, with only one property added. If A changes, B changes.- I could just use a construct to set the values, but I prefer something more automatic. I currently just 'copy' the properties one by one, this is doable but feels... cheap.
We've recently started to focus a bit more on proper programming patterns, this feels like there is one we're missing.