# [RFC] unset(): return bool if the variable has existed

- Thread: https://externals.io/message/66500
- Original archive: https://news-web.php.net/php.internals/66500
- Started by Bob Weinand on 2013-03-06T21:10:29+00:00
- Messages: 22
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #66500 — Bob Weinand — 2013-03-06T21:10:29+00:00

URL: https://externals.io/message/66500#66500

Hi!

As this seem to require a RFC, here is it:

https://wiki.php.net/rfc/unset_bool

Please feedback,

Bob Weinand

---

## #66501 — Steve Clay — 2013-03-06T21:24:33+00:00

URL: https://externals.io/message/66500#66501 · In reply to: #66500

> https://wiki.php.net/rfc/unset_bool

What's the return value of unset($setValue, $undefined) ?


Steve Clay
-- 
http://www.mrclay.org/

---

## #66502 — Bob Weinand — 2013-03-06T21:31:09+00:00

URL: https://externals.io/message/66500#66502 · In reply to: #66501

false. It's like unset($setValue) && unset($undefined).

I've clarified this in the RFC; thank you.

Bob Weinand

Am 06.03.2013 um 22:24 schrieb "Steve Clay" <steve@mrclay.org>:

> 
> > […]
> 
> What's the return value of unset($setValue, $undefined) ?
> 
> 
> Steve Clay
> -- 
> http://www.mrclay.org/

---

## #66503 — Florin Razvan Patan — 2013-03-06T21:34:15+00:00

URL: https://externals.io/message/66500#66503 · In reply to: #66502

Hello,


> false. It's like unset($setValue) && unset($undefined).
>
> I've clarified this in the RFC; thank you.
>
> Bob Weinand
>
> Am 06.03.2013 um 22:24 schrieb "Steve Clay" <steve@mrclay.org>:
>
> > […]

Would it make more sense to return an array with keys the name of the
variables that were unset and the result for each of them? This way
one could better handle 'false' being returned.

I'm not sure if it's possible or not, I didn't had the time to check
out the patch.


Thanks
----
Florin Patan
https://github.com/dlsniper

---

## #66504 — Anthony Ferrara — 2013-03-06T21:36:28+00:00

URL: https://externals.io/message/66500#66504 · In reply to: #66503

Florin


Would it make more sense to return an array with keys the name of the
> variables that were unset and the result for each of them? This way
> one could better handle 'false' being returned.
>
> I'm not sure if it's possible or not, I didn't had the time to check
> out the patch.
>

If you need that much granularity, just call unset() individually for each
one...

Anthony

---

## #66505 — Ferenc Kovacs — 2013-03-06T21:38:55+00:00

URL: https://externals.io/message/66500#66505 · In reply to: #66500

> Hi!
>
> As this seem to require a RFC, here is it:
>
> https://wiki.php.net/rfc/unset_bool
>
> Please feedback,
>
> Bob Weinand
>
> --
> 
> 
>
>
unset is not a function, but a language construct, and there is at least
one similar construct which doesn't return anything (eg. not returning
NULL, but syntax error when used in a return context): echo

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #66506 — Will Fitch — 2013-03-06T21:39:35+00:00

URL: https://externals.io/message/66500#66506 · In reply to: #66500

> Hi!
>
> As this seem to require a RFC, here is it:
>
> https://wiki.php.net/rfc/unset_bool


I'm not a fan of this change, but if it's going to be discussed, the RFC
should include baseline and RFC change benchmarks.


>
>
> Please feedback,
>
> Bob Weinand

---

## #66507 — Ferenc Kovacs — 2013-03-06T21:40:17+00:00

URL: https://externals.io/message/66500#66507 · In reply to: #66505

>
>
>
> 
>
> > […]
> unset is not a function, but a language construct, and there is at least
> one similar construct which doesn't return anything (eg. not returning
> NULL, but syntax error when used in a return context): echo
>
>
referring to "This removes also an inconsistency: the function's return
value is void, states the docs. It is until now the only function which has
no return value." especially.

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #66508 — Will Fitch — 2013-03-06T21:42:58+00:00

URL: https://externals.io/message/66500#66508 · In reply to: #66507

> 
>
> > […]
> wrote:
> > […]
> referring to "This removes also an inconsistency: the function's return
> value is void, states the docs. It is until now the only function which has
> no return value." especially.
>

Agreed.  The RFC needs to be updated as to not mislead readers.


>
> --
> Ferenc Kovács
> @Tyr43l - http://tyrael.hu

---

## #66509 — Bob Weinand — 2013-03-06T21:49:44+00:00

URL: https://externals.io/message/66500#66509

Am 06.03.2013 um 22:47 schrieb "Ferenc Kovacs" <tyra3l@gmail.com>:

> 
> 
> 
> 
> Am 06.03.2013 um 22:38 schrieb "Ferenc Kovacs" <tyra3l@gmail.com>:
> 
> > […]
> 
> 
> Echo doesn't behave as a function as it doesn't need parenthesis etc. (and has an equivalent which can be used as alternative: print)
> 
> I'd consider unset as a function in form of a language construct.
> 
> Bob Weinand
> 
> I'm not sure that we should change our terminology based on whether or not a language construct requires the parenthesis or not.
> there are other differences in behavior, for example $foo = "print";$foo("bar"); won't work even thought that print("bar"); look likes as a function call.
> 
> 
> -- 
> Ferenc Kovács
> @Tyr43l - http://tyrael.hu

Ok; I think it's better not to argue about this...

Bob Weinand

---

## #66510 — Ferenc Kovacs — 2013-03-06T21:53:56+00:00

URL: https://externals.io/message/66500#66510 · In reply to: #66509

> Am 06.03.2013 um 22:47 schrieb "Ferenc Kovacs" <tyra3l@gmail.com>:
>
>
>
>
> 
>
> > […]
>
> I'm not sure that we should change our terminology based on whether or not
> a language construct requires the parenthesis or not.
> there are other differences in behavior, for example $foo =
> "print";$foo("bar"); won't work even thought that print("bar"); look likes
> as a function call.
>
>
> --
> Ferenc Kovács
> @Tyr43l - http://tyrael.hu
>
>
> Ok; I think it's better not to argue about this...
>
> Bob Weinand
>

I'm glad that we sorted this out.

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #66511 — Bob Weinand — 2013-03-06T22:25:16+00:00

URL: https://externals.io/message/66500#66511

Am 6.3.2013 um 22:50 schrieb Will Fitch <willfitch@php.net>:

> 
> Am 06.03.2013 um 22:39 schrieb "Will Fitch" <willfitch@php.net>:
> 
> > […]
> 
> I don't see here a real need for a benchmark as it is mostly only returning SUCCESS or FAILURE instead of nothing. Nothing what would slow PHP down.
> 
> Any change to a language construct which requires additional processing adds time.  It may be in minute, but it's part of the discussion.  Since this core change you're requesting comments for introduces a usecase which many will not find useful (probably most voting), it is your responsibility to convince this category of people that the tradeoff won't affect them that much.
> 
> If you choose not to add benchmarking, I will guarantee a -1 from me.
>  
> 
> Bob Weinand


I have tried the following:

time ./sapi/cli/php -r 'while($i++ < 1e7) { $a = true; unset($a); }'

Unpatched: average of 5x executed:
real	0m4.935s
user	0m4.925s
sys	0m0.008s

Patched: average of 5x executed:
real	0m4.945s
user	0m4.938s
sys	0m0.005s


Yes, there is an increase of 0.15%. This is 1 nanosecond more than previous.

Is this exact enough? Or do you need more precision?

If yes, I'll put this into the RFC.


Bob Weinand

---

## #66512 — Will Fitch — 2013-03-06T22:30:42+00:00

URL: https://externals.io/message/66500#66512 · In reply to: #66511

> Am 6.3.2013 um 22:50 schrieb Will Fitch <willfitch@php.net>:
>
> 
>
> > […]
>
> Any change to a language construct which requires additional processing
> adds time.  It may be in minute, but it's part of the discussion.  Since
> this core change you're requesting comments for introduces a usecase which
> many will not find useful (probably most voting), it is your responsibility
> to convince this category of people that the tradeoff won't affect them
> that much.
>
> If you choose not to add benchmarking, I will guarantee a -1 from me.
>
>
> > […]
>
> I have tried the following:
>
> time ./sapi/cli/php -r 'while($i++ < 1e7) { $a = true; unset($a); }'
>
> Unpatched: average of 5x executed:
> real 0m4.935s
> user 0m4.925s
> sys 0m0.008s
>
> Patched: average of 5x executed:
> real 0m4.945s
> user 0m4.938s
> sys 0m0.005s
>
>
> Yes, there is an increase of 0.15%. This is 1 nanosecond more than
> previous.
>
> Is this exact enough? Or do you need more precision?
>
> If yes, I'll put this into the RFC.
>

Thank you.  Please do add to the RFC


>
>
> Bob Weinand

---

## #66513 — Bob Weinand — 2013-03-06T22:36:25+00:00

URL: https://externals.io/message/66500#66513 · In reply to: #66512

Am 6.3.2013 um 23:30 schrieb Will Fitch <wfitch@meetme.com>:
> 
> Am 6.3.2013 um 22:50 schrieb Will Fitch <willfitch@php.net>:
> > […]
> 
> 
> I have tried the following:
> 
> time ./sapi/cli/php -r 'while($i++ < 1e7) { $a = true; unset($a); }'
> 
> Unpatched: average of 5x executed:
> real	0m4.935s
> user	0m4.925s
> sys	0m0.008s
> 
> Patched: average of 5x executed:
> real	0m4.945s
> user	0m4.938s
> sys	0m0.005s
> 
> 
> Yes, there is an increase of 0.15%. This is 1 nanosecond more than previous.
> 
> Is this exact enough? Or do you need more precision?
> 
> If yes, I'll put this into the RFC.
> 
> Thank you.  Please do add to the RFC
>  
> 
> 
> Bob Weinand

RFC updated.

Any other comments about this RFC?

Bob Weinand

---

## #66514 — Stas Malyshev — 2013-03-06T23:32:44+00:00

URL: https://externals.io/message/66500#66514 · In reply to: #66513

Hi!

> RFC updated.
> 
> Any other comments about this RFC?

Could you provide a use case for this - which practical value this has?

It also still contains factually incorrect claim that unset() is a
function and that there's some "inconsistency" in the fact that it does
not return value.

Also, it is not clear what false returned from unset() actually means -
did it fail to unset the value (i.e., it is still set) or there was
nothing to unset (i.e., it is still not set)?
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #66516 — Sebastian Krebs — 2013-03-07T08:45:53+00:00

URL: https://externals.io/message/66500#66516 · In reply to: #66514

2013/3/7 Stas Malyshev <smalyshev@sugarcrm.com>

> Hi!
>
> > […]
>
> Could you provide a use case for this - which practical value this has?
>
> It also still contains factually incorrect claim that unset() is a
> function and that there's some "inconsistency" in the fact that it does
> not return value.
>
> Also, it is not clear what false returned from unset() actually means -
> did it fail to unset the value (i.e., it is still set)


Shouldn't this fail a little bit more obvious (-> "loud")? And how is this
even possible?


> or there was
> nothing to unset (i.e., it is still not set)?
>

So I guess this is the only useful behaviour. However, I have no idea, what
this information should tell me. If I call unset() then I want to ensure,
that the variable is not set anymore, but for what reason I should need to
know, whether it was set before, or not?

Regards,
Sebastian


> --
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/
> (408)454-6900 ext. 227
>
> --
> 
> 
>
>


-- 
github.com/KingCrunch

---

## #66517 — Stas Malyshev — 2013-03-07T08:53:27+00:00

URL: https://externals.io/message/66500#66517 · In reply to: #66516

Hi!

> Shouldn't this fail a little bit more obvious (-> "loud")? And how is
> this even possible?

Well, for example - __unset is required to do X before unsetting
variable but X fails for one reason or another and the logic dictates
that you can not unset unless X is done (for a real life example, try
exiting an app like word processor with content unsaved and when it asks
you to save/discard/cancel, click on cancel. Bingo - unset interrupted :).

>     or there was
>     nothing to unset (i.e., it is still not set)?
> 
> 
> So I guess this is the only useful behaviour. However, I have no idea,

This behavior is already covered by isset, why duplicate it? If - and I
have no idea why - you cared if the variable was set or not, why not
just do isset?

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #66518 — Marco Pivetta — 2013-03-07T08:54:02+00:00

URL: https://externals.io/message/66500#66518 · In reply to: #66516

>
> So I guess this is the only useful behaviour. However, I have no idea, what
> this information should tell me. If I call unset() then I want to ensure,
> that the variable is not set anymore, but for what reason I should need to
> know, whether it was set before, or not?
>

I think I actually had an use case with proxies, where I wanted to know if
a property was set or not. That's useful to reset the proxy state
(basically to intercept property access) and differentiate null properties
vs non existing properties:

    class Blah extends Blubb
    {
        protected $prop;
        public function __construct() { unset($this->prop); }
        public function init() { $this->prop = 'stuff'; }
        public function `reset()` { return unset($this->prop); } // we now
know if `init` was called
    }

This *could* be useful if I want to keep track of the state of a proxy, but
it's just an additional property to keep track of manually on the other
side. The additional property is also more explicit and easier to track.

Yes, it needs a better use case.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

---

## #66519 — Bob Weinand — 2013-03-07T12:10:43+00:00

URL: https://externals.io/message/66500#66519 · In reply to: #66514

Am 7.3.2013 um 00:32 schrieb Stas Malyshev <smalyshev@sugarcrm.com>:

> Hi!
> 
> > […]
> 
> Could you provide a use case for this - which practical value this has?
> 
> It also still contains factually incorrect claim that unset() is a
> function and that there's some "inconsistency" in the fact that it does
> not return value.
> 
> Also, it is not clear what false returned from unset() actually means -
> did it fail to unset the value (i.e., it is still set) or there was
> nothing to unset (i.e., it is still not set)?
> -- 
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/
> (408)454-6900 ext. 227
> 
> -- 
> 
> 

Hi!

The main practical value is in the __unset magic method. You can now communicate through the "proper" way of a language construct with an __unset method. (success or failure)

I've added a code example to the RFC.

Bob Weinand

---

## #66539 — Stas Malyshev — 2013-03-07T19:57:24+00:00

URL: https://externals.io/message/66500#66539 · In reply to: #66519

Hi!

> The main practical value is in the __unset magic method. You can now
> communicate through the "proper" way of a language construct with an
> __unset method. (success or failure)

I'm not sure how useful is that based on your example - unset could
throw the exception as well... And the whole case seems still unclear -
what is "false" supposed to mean in general case? Property didn't exist?
Property can not be unset due to special semantics?
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #66540 — Will Fitch — 2013-03-07T20:06:53+00:00

URL: https://externals.io/message/66500#66540 · In reply to: #66519

> Am 7.3.2013 um 00:32 schrieb Stas Malyshev <smalyshev@sugarcrm.com>:
>
> > […]
>
> Hi!
>
> The main practical value is in the __unset magic method. You can now
> communicate through the "proper" way of a language construct with an
> __unset method. (success or failure)
>

The practical value in __unset is that you can unset an unavailable
"property".  The goal of unset has never been to communicate anything.  It
simply destroys a variable.  Furthermore, if you're looking to identify,
based on your examples, if you need to do cleanup work if a variable is
set, your application should check to see if that value is set.  Unset is
part of the cleanup work itself.

It would be nice to see actual use cases in an application rather than
examples showing that your very specific use cases would work with this
change.  I've yet to see any value added by this RFC.


>
> I've added a code example to the RFC.
>
> Bob Weinand

---

## #66541 — Bob Weinand — 2013-03-07T20:10:16+00:00

URL: https://externals.io/message/66500#66541

cc'ing the internals
__

Am 07.03.2013 um 20:57 schrieb "Stas Malyshev" <smalyshev@sugarcrm.com>:

> Hi!
> 
> > […]
> 
> I'm not sure how useful is that based on your example - unset could
> throw the exception as well... And the whole case seems still unclear -
> what is "false" supposed to mean in general case? Property didn't exist?
> Property can not be unset due to special semantics?
> -- 
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/
> (408)454-6900 ext. 227

Take for example the unset method of a framework. Which return false. Then you have an abstraction layer which converts the false into an exception. Yes, in this simple use case it may be better to throw the exception directly. But as you begin to introduce these well-known design patterns there may appear some sense...

Unset returns false if there was nothing to unset or if an unset_dimension/property handler has returned false. Short: It doesn't return true if nothing was successfully unset.

Bob Weinand
