Re: [RFC][Vote] Typed Properties

From: Date: Fri, 27 May 2016 09:16:49 +0000
Subject: Re: [RFC][Vote] Typed Properties
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19  Groups: php.internals 
Request: Send a blank email to internals+get-93601@lists.php.net to get a copy of this message
On 27/05/2016 09:35, Tony Marston wrote:
"Lester Caine" wrote in message news:5747F02A.2010808@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]

« previous php.internals (#93601) next »