Re: How to cast objects in arrays.
| From: | Alberto Serra | Date: | Fri, 05 Jul 2002 06:34:40 +0000 |
| Subject: | Re: How to cast objects in arrays. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-105573@lists.php.net to get a copy of this message | ||
Anas Mughal wrote:
I have a function getRow() that returns a new instance of an object called IvActivity. I insert this new instance into $list. I need to call getTitle method on each of the inserted objects. However, I keep on getting parse error on the line that does the casting.
$i = 0; while ($row = mysql_fetch_array($mysql_result)) { $instance = $this->getRow($row); $list[$i] = $intance; $i++; }I take this is just a typo in your example. $list[$i] = $intance; should really be: $list[$i] = &$instance; by not using the ampersand you pass a COPY of the object and not the object you built. Maybe it doesn't do any harm in your application, but it tends to be dangerous.
foreach ($list as $item) { $act = (IvActivity) $item; echo "item: " . $act->getTitle(); }this may just be: foreach ($list as $item) { echo "item: " . $item->getTitle(); } Because you said that:
getRow() returns a new instance of an object called IvActivity.Watch out for copies again. I am not sure whether this will execute the object instead of a fresh copy. Again, this might mean nothing in your context. Alberto Kiev -- @-_=}{=_-@-_=}{=_-@-_=}{=_-@-_=}{=_-@-_=}{=_-@-_=}{=_-@-_=}{=_-@ LoRd, CaN yOu HeAr Me, LiKe I'm HeArInG yOu? lOrD i'M sHiNiNg... YoU kNoW I AlMoSt LoSt My MiNd, BuT nOw I'm HoMe AnD fReE tHe TeSt, YeS iT iS ThE tEsT, yEs It Is tHe TeSt, YeS iT iS ThE tEsT, yEs It Is.......