Re: how to mark protected properties?

From: Date: Tue, 08 Jun 2004 13:58:33 +0000
Subject: Re: how to mark protected properties?
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-30170@lists.php.net to get a copy of this message
Tomas V.V.Cox wrote: >Bertrand Mansion wrote: >> Tomas V.V.Cox wrote: >> >> >>>Bertrand Mansion wrote: >>> >>>>Tomas V.V.Cox wrote: >>>> >>>> >>>>>IMHO, as silly as sounds to prefix all vars with '_', sounds to create >>>>> >>>>>dozen methods/calls for getting access to those protected vars. >>>> >>>> >>>>If you are clever, all you need is 2 methods. >>>>That's called key-value coding. >>> >>>Please read it again: "methods/calls" (replace "/" with >>>"or"), specially >>>the "calls". Now imagine the cost inside a > 100 loop, any basic php >>>performance tutorial will tell you it. >> >> >> I am not sure you understand what protected is ? >> >> You can still do your > 100 loop with $this->foo = value if you need to. >> If you need to do a > 100 loop somewhere outside your object, just pass the >> values to iterate by reference as an array to your object and let it do the >> assignment job. I don't see a performance problem with that. Maybe a less >basic >> tutorial could tell you about it. > >Let's talk with some code then, 10 times slower is not a performance >problem? If you mean that objA needs to set an instance var value in objB through an accessor inside of a > 10000000 loop, then I'd say the ratio is not 10 times but 2-2.5 times slower which, I agree, is still a lot. But I can't think of examples where this couldn't be handled with a better design of your class. For example, $res = $db->query($query); $obj->setOptions($res); or : $arr = $db->getAll($query); $obj->setOptions($arr); instead of : while ($row = $res->fetchRow()) { $obj->addOption($row); } instead of : while ($row = $res->fetchRow()) { $obj->options[] = $row; } As you might guess, there is no performance hit in the first and third solution while there is in the second solution. Still, you can use the third solution anywhere in your subclasses. Outside, you use the first solution. This looks cleaner to me and it is the way to go if you feel concerned by performance in big loops. If you have examples where solution 2 is the only solution, I would be interested to see them. Bertrand Mansion Mamasam

« previous php.pear.dev (#30170) next »