472,958 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

how much can unset do?

Given a bunch of mixes variables, I'm in a situation where I can't
know what type they are.

Can I use unset to kill an object? unset($object);

Can I use unset to kill a pointer to an open file? unset($fp);

Can I use unset to kill a pointer to a database return?
unset($mySqlResult);

Because of heavy masking in my objects, I often don't know what type
of variable I've got. In particular, I don't if the resource I've got
is a file pointer or a database pointer, all I know is that is fetched
data from some datastore. So, given the uncertainty, is unset a
reasonable way to kill these variables?
Jul 17 '05 #1
7 2082
lawrence wrote:
Given a bunch of mixes variables, I'm in a situation where I can't
know what type they are.

Can I use unset to kill an object? unset($object);

Can I use unset to kill a pointer to an open file? unset($fp);

Can I use unset to kill a pointer to a database return?
unset($mySqlResult);

Because of heavy masking in my objects, I often don't know what type
of variable I've got. In particular, I don't if the resource I've got
is a file pointer or a database pointer, all I know is that is fetched
data from some datastore. So, given the uncertainty, is unset a
reasonable way to kill these variables?


Taken from the PHP manual:!

"unset() destroys the specified variables. Note that in PHP 3, unset()
will always return TRUE (actually, the integer value 1). In PHP 4,
however, unset() is no longer a true function: it is now a statement. As
such no value is returned, and attempting to take the value of unset()
results in a parse error."

Jul 17 '05 #2
Uzytkownik "lawrence" <lk******@geocities.com> napisal w wiadomosci
news:da**************************@posting.google.c om...
Given a bunch of mixes variables, I'm in a situation where I can't
know what type they are.

Can I use unset to kill an object? unset($object);
Not always. And since PHP4 doesn't have destructor, it doesn't make much
sense to talk about "killing" an object.
Can I use unset to kill a pointer to an open file? unset($fp);
No.
Can I use unset to kill a pointer to a database return?
unset($mySqlResult);
No.
Because of heavy masking in my objects, I often don't know what type
of variable I've got. In particular, I don't if the resource I've got
is a file pointer or a database pointer, all I know is that is fetched
data from some datastore. So, given the uncertainty, is unset a
reasonable way to kill these variables?


No. Just let the script run to completion and all resource will be freed.
Jul 17 '05 #3
"Chung Leong" <ch***********@hotmail.com> wrote in message
Because of heavy masking in my objects, I often don't know what type
of variable I've got. In particular, I don't if the resource I've got
is a file pointer or a database pointer, all I know is that is fetched
data from some datastore. So, given the uncertainty, is unset a
reasonable way to kill these variables?


No. Just let the script run to completion and all resource will be freed.


That will work 99% of the time. The time is won't is when an object
has been assigned a var, and does some operation, and then, during the
next operation, the attempt to assign a value to that variable fails,
so the variable now has the value from the first operation, instead of
the second one. I can't protect myself against that through any easy
kind of test, like isset() or is_array(), because if it was set with
the right type the first time it will still set true. So at the
beginning of the method that sets that variable I've been doing
unset(), to first clear the var, before setting it again. Does unset
protect in such situations?
Jul 17 '05 #4
Yes. Unset() removes the variable from the current namespace, so that when
it's used again, a new variable is created. Unset() does not free the
underlying resource, however. So you files woud be left opened and the
memory allocated for result set won't be freed (until the script is done
running).

It seems your object code has some major flaws. If you're going to
encapsulate functionalities you need to do it completely. You should never
have a situation where you don't know what something is but need to know.
Methods that return different resource types are particularly bad, since
they're completely unrelated.

Uzytkownik "lawrence" <lk******@geocities.com> napisal w wiadomosci
news:da**************************@posting.google.c om...
"Chung Leong" <ch***********@hotmail.com> wrote in message
Because of heavy masking in my objects, I often don't know what type
of variable I've got. In particular, I don't if the resource I've got
is a file pointer or a database pointer, all I know is that is fetched
data from some datastore. So, given the uncertainty, is unset a
reasonable way to kill these variables?
No. Just let the script run to completion and all resource will be

freed.
That will work 99% of the time. The time is won't is when an object
has been assigned a var, and does some operation, and then, during the
next operation, the attempt to assign a value to that variable fails,
so the variable now has the value from the first operation, instead of
the second one. I can't protect myself against that through any easy
kind of test, like isset() or is_array(), because if it was set with
the right type the first time it will still set true. So at the
beginning of the method that sets that variable I've been doing
unset(), to first clear the var, before setting it again. Does unset
protect in such situations?

Jul 17 '05 #5
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<K6********************@comcast.com>...
It seems your object code has some major flaws. If you're going to
encapsulate functionalities you need to do it completely. You should never
have a situation where you don't know what something is but need to know.
Methods that return different resource types are particularly bad, since
they're completely unrelated.
Yes, you're right. After thinking about it, I've no idea why I'm
trying to kill the resource up in the object that isn't supposed to
know anything about the details of the implementation or the
underlying datastore. I'm going to change the code so that the request
merely gets passed down to the object that is specific to the
datastore:

$this->datastoreObject->destroyResource();

The datastoreObject will always know which type of datastore it is
dealing with, and whether it is a MySql resource or a file pointer,
and it will know how to behave appropriately.

Thanks for pointing that out.



Uzytkownik "lawrence" <lk******@geocities.com> napisal w wiadomosci
news:da**************************@posting.google.c om...
"Chung Leong" <ch***********@hotmail.com> wrote in message
> Because of heavy masking in my objects, I often don't know what type
> of variable I've got. In particular, I don't if the resource I've got
> is a file pointer or a database pointer, all I know is that is fetched
> data from some datastore. So, given the uncertainty, is unset a
> reasonable way to kill these variables?

No. Just let the script run to completion and all resource will be

freed.

That will work 99% of the time. The time is won't is when an object
has been assigned a var, and does some operation, and then, during the
next operation, the attempt to assign a value to that variable fails,
so the variable now has the value from the first operation, instead of
the second one. I can't protect myself against that through any easy
kind of test, like isset() or is_array(), because if it was set with
the right type the first time it will still set true. So at the
beginning of the method that sets that variable I've been doing
unset(), to first clear the var, before setting it again. Does unset
protect in such situations?

Jul 17 '05 #6
lk******@geocities.com (lawrence) wrote in message
news:<da**************************@posting.google. com>...

Given a bunch of mixes variables, I'm in a situation where I can't
know what type they are.


Of course you can. Use gettype()...

Cheers,
NC
Jul 17 '05 #7
nc@iname.com (Nikolai Chuvakhin) wrote in message news:<32**************************@posting.google. com>...
lk******@geocities.com (lawrence) wrote in message
news:<da**************************@posting.google. com>...

Given a bunch of mixes variables, I'm in a situation where I can't
know what type they are.


Of course you can. Use gettype()...

And get_resource_type() for the types of resource? I've thought about
that, but it gets messy - I end up with a long switch statement
dealing with each type, and I may miss a few, and the PHP crew might
add new types in the future, so I'd have to change one of my core
classes.

I think I like the suggestion someone else made, that this kind of
thing simply doesn't belong that high in the class hierarchy. I should
pass the request along to the low level object that knows what type it
is dealing with. That way if changes arise in the future I only have
to change some subclass. I don't have to make any changes in my core
class, which would be dangerous.
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
PHP: unset($_SESSION); In my original environment (PHP 4.3.2) this line will delete the session variable 'mainDisplay'. But in the testing environment (PHP 4.3.6) the variable persists even...
2
by: Steve | last post by:
I'm working on an e-commerce site, and one of the things I need to do is split an existing order into two orders. The problem I'm having is not creating the new order, but getting the remaining...
5
by: ahevans | last post by:
Howdy all, I'm progressing with PHP but I have 2 questions that are kinda bugging me. Say for example, I have a php page similar to this.... if ..... if........
21
by: steve | last post by:
Dontí make my mistake. It is costly. Say you have defined a variable $var in your main script. Now in a function you access it using: global $var; But you want to set it to null inside the...
3
by: fasanay | last post by:
Hi everybody I have got the following PHP code which I am trying to convert to ASP any help will be appreciated...I have done most of it but I cant find a replace function for Unset in asp which...
1
by: frizzle | last post by:
Hi group, Why won't $new_var be unset in the following function? Am i missing out something? Greetings Frizzle. *************************************
7
by: fasanay | last post by:
Hi everybody I have got the following PHP code which I am trying to convert to ASP any help will be appreciated...I have done most of it but I cant find a replace function for Unset in asp which...
5
by: comp.lang.php | last post by:
// NEW 11/27/2006: FINALLY, IF YOU ADDED OR DELETED OR DID ANY KIND OF FORM ACTION SUCCESSFULLY, DON'T RE-DISPLAY THE NEW EXPENSE ITEMS VIA $_POST if ($_POST && (!is_array($leaseObj->errorArray)...
0
Airslash
by: Airslash | last post by:
Hello, I've written a small function to delete a variable from a class' internal array. The variables on their own are custom class objects, and I'm a bit confused about the whole pass by...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.