473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 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 2102
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.