[nycphp-talk] multiple object instantiations in a single script
Matthew Terenzio
webmaster at localnotion.com
Sat Oct 9 14:36:25 EDT 2004
On Oct 9, 2004, at 7:57 AM, Dan Cech wrote:
> Matthew Terenzio wrote:
>> What a weird subject line. Is it an OOP or procedural question?
>> What I mean is. . .
>> Well, suppose you have a User class and a register() function in the
>> class.
>> Then on a given script/page you are looping through some values and
>> you want to create a new User and register it every time through the
>> loop.
>> Of course it doesn't work because you can't do something like:
>> $name = new User;
>> more than once.
>
> Actually, this will work just fine.
Fair enough. But say you have this situation.
function doSomething() {
$obj = new object;
$obj-> doing something
other things happening
$obj->destruct;
}
what happens if the script calls doSomething a second time before $obj
is done. Am I in some danger of overwriting $obj before I'm through
using the previous instance?
if not, Is the same true if the instantiation happens outside of a
function?
I guess the distilled question is:
When the engine instantiates a new object, is the memory allocated to
that object unique, both from within a given script and across multiple
scripts, if indeed that object is passed around an application?
>
>> 1) Do most people use dynamically named variables?
>
> Very rarely, it's generally clearer to use an array if you want to
> keep the variable for later, or just unset/reuse it.
>
> ie:
>
> $users = array();
> foreach ($signups as $id => $signup) {
> $users[$id] = new User();
> $users[$id]->register();
> }
>
> or:
>
> foreach ($signups as $signup) {
> $user = new User();
> $user->register();
> unset($user);
> }
>
> the call to unset is optional, if you leave it out the variable $user
> will be overwritten on each iteration through the loop.
>
> Dan
> _______________________________________________
> New York PHP Talk
> Supporting AMP Technology (Apache/MySQL/PHP)
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.newyorkphp.org
More information about the talk
mailing list