473,659 Members | 2,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($mySqlRes ult);

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 2124
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($mySqlRes ult);

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******@geoci ties.com> napisal w wiadomosci
news:da******** *************** ***@posting.goo gle.com...
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($mySqlRes ult);
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******@geoci ties.com> napisal w wiadomosci
news:da******** *************** ***@posting.goo gle.com...
"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******* *************@c omcast.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->datastoreObjec t->destroyResourc e();

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******@geoci ties.com> napisal w wiadomosci
news:da******** *************** ***@posting.goo gle.com...
"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******@geocit ies.com (lawrence) wrote in message
news:<da******* *************** ****@posting.go ogle.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.go ogle.com>...
lk******@geocit ies.com (lawrence) wrote in message
news:<da******* *************** ****@posting.go ogle.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_ty pe() 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
14903
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 after unset() is called. Is there some special setting in PHP that can allow a session to
2
3130
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 items from the original order cleaned up in the array. What I've tried to do so far is: 1) The data is stored in a serialized array in the order_data field in the orders table. When the order is selected, it is unserialized and called $order_data....
5
2840
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
3381
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 function. DO: $var = null; DONT DO: unset($var);
3
4107
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 will discard the variable alltogether... if ($categoryid == "all") { $sql = "SELECT * FROM products where shopinspection=$shopinspection"; unset($HTTP_POST_VARS); unset($HTTP_POST_VARS);
1
1884
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
1851
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 will discard the variable alltogether... if ($categoryid == "all") { $sql = "SELECT * FROM products where shopinspection=$shopinspection"; unset($HTTP_POST_VARS); unset($HTTP_POST_VARS);
5
2292
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) || @sizeof($leaseObj->errorArray) == 0)) { print_r(array_keys($_POST)); @reset($_POST); $tempPost = $_POST; foreach ($_POST as $key =$val) if (strpos($key, 'new_') === 0) array_remove($tempPost, $tempPost);
0
1413
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 reference thing. First I'll show you the code: # removes a parameter from the array. # variables are passed as reference to prevent overhead.
0
8427
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8850
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.