On 5/27/2016 11:53 AM, Lester Caine wrote: > On 27/05/16 10:16, Rowan Collins wrote: >> My comment was in reaction to Lester expressing surprise at destroying >> and re-creating objects when new data is fetched. To be clear, I'm >> talking about this: >> >> // Clear old data >> $this->data = []; >> // Re-populate from new DB result >> foreach ( $db_resultset as $row ) { >> $this->data[] = new SomeModel($row); >> } >> >> The most useful typehints are inside the SomeModel class, not the >> table-level wrapper. This is true even if they're just dumb structs, and >> all the behaviour is at the table level, because it presumably has >> basically one field: array $data. > > Probably the only difference here is that '$row' in my result set is not > a dumb structure. It has type information, field lengths and potentially > even value constraints. So my table level wrapper can potentially have > the same typehints that you are then adding to SomeModel. All the > properties of SomeModel already exist and if typed properties are > working correctly in PHP $row can simply be accessed directly. This is > why in my book all of the debate about 'attributes' and everything else > is so tied up in the definition of making a simple variable a much more > flexible generic object. >
The type hints belong to SomeModel because you want to insert the data into SomeModel and not into your repository (Table). class Person { public int $id; public DateTimeImmutable $created; public DateTimeImmutable $changed; public string $name; public DateTimeImmutable $birth; public ?DateTimeImmutable $marriage; public ?DateTimeImmutable $death; public int $number_of_siblings = 0; } class SomeModels { public function findAll() { foreach ($db->select('SELECT * FROM `SomeModel`') as $row) { $some_model = new SomeModel; $some_model->id = $row['id']; $some_model->created = new DateTimeImmutable($row['created']); $some_model->changed = new DateTimeImmutable($row['changed']); $some_model->name = $row['name']; $some_model->birth = new DateTimeImmutable($row['birth']); if (isset($row['marriage'])) { $some_model->marriage = new DateTimeImmutable($row['marriage']); } if (isset($row['death'])) { $some_model->death = new DateTimeImmutable($row['death']); } $some_model->number_of_siblings = $row['number_of_siblings']; yield $some_model; } } } The Person object is however still behaviorless and anemic, throwing type hints at public properties does not change that fact. -- Richard "Fleshgrinder" Fussenegger
signature.asc
Description: OpenPGP digital signature