Le Fri, 18 Sep 2015 20:38:12 +0200, Rowan Collins <rowan.coll...@gmail.com> a écrit:

Benoit Schildknecht wrote on 18/09/2015 19:24:
I agree. exists() would allow PHP devs to make more secure, cleaner and lighter code. It fills a gap and would be useful to a lot of devs.

On the contrary, I think code using exists() would be extremely fragile, because it's highly resistant to refactoring, and encourages developers to leave variables uninitialised.

What makes cleaner, more secure code is always initialising your variables before using them - basically, what the "undefined variable" Notice is suggesting you do.

The examples Lester and Robert have come up with make sense in themselves, but are not architectures I would choose precisely because they are opaque (you have to remember what unset() and null mean) and hard to extend (there's no way to introduce a 4th state alongside the unset/null/value triad).

Obviously, all language features can be used to write bad code, but it seems much easier to write bad code than good using this proposed function.

Regards,

So there is a bug regarding variables and null. Because I don't get any notice if I use a variable with "null" as a value. If "null" means "uninitialized", how come there are no notices ?
<?php
echo $foo;//Notice
$foo = null;
echo $foo;//No Notice

And how come the null variable appear in get_defined_vars(), and disappear after an unset() ? Because null != undefined. The engine seems to consider null as a distinct state. Something that is not undefined. It is already like that, we are not theorizing.
<?php
var_dump(get_defined_vars()); // No traces of $foo
$foo = null;
var_dump(get_defined_vars()); // ["foo"] => NULL
unset($foo);
var_dump(get_defined_vars()); // Good bye, foo

You have a lot of people who use isset() against null elements in an array. null elements happen a lot when interacting with an SQL server (for instance). I personally use array_key_exists(), but most people won't do the same at all. Because they think it is the same. And they'll forget, because isset() behaviour doesn't make sense, since null doesn't exactly behave as you say.

Using is_null() against a var to see if it is set and has a null value WILL trigger notices, and that's clearly not clean at all. In a production environment, you would like to get rid of them. All of them.

Taking advantage of set_error_handler() is out of question : how can you know if the triggered notice is related to a variable you expected to be declared with null ? Very hard task (if not impossible), and very heavy to implement.

And you have webservices, cURL, JSON, etc... All of these can give you "null" values. And it is not rare. This is not something we can correct. We have no control on this.

There are way more cases in which exists() would improve things A LOT. We need a native function that allows us to check this state WITHOUT triggering a notice.

Regards,

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to