You are here: Re: How to emulate isset() ? (is it possible?) « PHP Programming Language « IT news, forums, messages
Re: How to emulate isset() ? (is it possible?)

Posted by gosha bine on 05/29/07 19:42

Giovanni R. wrote:
> gosha bine <stereofrog@gmail.com> wrote:
>
>> what exactly is the desired behaviour of that new, emulated isset?
>
> The fact is that I'm lazy. ;-)
>
> I'd like to use a single function - a kind of wrapper for isset() - to
> check whether the $var isset(), and to sanitize it according to my will.
>
> Something like this:
>
> function wrapper(&$var) {
>
> if ( !isset($var) || !strlen($var) ) return '';
>
> $var = trim ($var);
>
> // other checks
>
> return $var;
>
> }
>
> Here it is how it could be used:
>
> print wrapper($array['inexistent_index']);
>
> In this way, even if $array['inexistent_index'] isn't defined, I don't
> get a notice. The fact is that wrapper() adds that index to the array
> and sets $array['inexistent_index'] to NULL. :-(
>
> So I was asking myself how isset() works and if a similar function could
> be developed using PHP or not.
>
> Giovanni
>
>

I'm afraid that's not possible, Giovanni. "isset" is a special function
in that it doesn't evaluate its argument before call. It isn't possible
to write such function in php.

The usual workarounds are to use '@' operator to suppress notices

wrapper(@$array['inexistent_index']);

or to split array[index] into two distinct arguments:

wrapper($array, 'inexistent_index');

Even better would be to use an OO wrapper with getter method(s)
"get($index)" or "getSomething".



--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация