On 27/05/2016 09:35, Tony Marston wrote:
"Lester Caine" wrote in message news:5747f02a.2010...@lsces.co.uk...
On 26/05/16 22:36, Rowan Collins wrote:
So every time I update the current tree because the client has selected
a different initial root I have to destroy perhaps 30 person objects
and
rebuild the whole lot again from scratch?
In a word, yes: if you have a different set of data, use a different set
of objects to hold it.
Objects are not memory slots to be re-filled, they represent a specific
instance of a thing.
I disagree completely. In a database system there is no rule that says
an object can only hold data from a single row otherwise Martin Fowler,
the author of PoEE, would not have patterns that hold data from several
rows. When I instantiate one of my table classes it is a valid but empty
object. There are two actions I can perform - inject data from the
client via the $_POST array, or tell it to retrieve data from the
database. This may involve just a single row, or multiple rows. When
reading from the database it may even result in zero rows.
That sounds like an object that represents the database table as an
entity, that's fine. What does it contain - how is the row data
represented? If it's just an array of arrays, then none of this
discussion is relevant; if it's an array of model objects, then those
objects must have properties, and those properties can be given type
annotations under this proposal.
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.
The alternative implied by Lester's comment seemed to be to somehow
re-use the entries in the data array by calling a magic "reset" method,
then populating them with new data. That's what I meant by "slots".
If I create a Person object representing "Lester Caine", it makes
absolutely no sense to go out of my way to recycle it and use it to
represent "Tony Marston"; that's a new value of the same type, so a new
instance of the same class.
Regards,
--
Rowan Collins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php