How to emulate isset() ? (is it possible?)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Giovanni R.

    How to emulate isset() ? (is it possible?)


    Take a look at this code (you can execute it):

    error_reporting (E_ALL);

    function byVal( $v) {}
    function byRef(&$v) {}

    print '<pre>';

    byVal ($first['inexistent_ind ex']); // gives a notice
    var_dump($first ); // gives a notice

    print '<hr />';

    byRef ($second['inexistent_ind ex']); // does NOT give a notice
    var_dump($secon d); // does NOT give a notice

    print '<hr />';

    isset($third); // does NOT give a notice
    var_dump ($third); // gives a notice

    print '</pre>';


    In the $first case, using byVal(), I get *two* notices.
    In the $second case, using byRef(), I get *zero* notice.
    In the $third case, using isset(), I get *one* notice.

    This means that:

    1) byVal() does NOT define the array and raises a notice
    (and var_dump() raises another notice).

    2) byRef() defines the array and does NOT raise notices
    (neither var_dump() raises a notice, since $second is defined).

    3) isset() does NOT define the array and does NOT raise notices
    (but var_dump() raises a notice, since $third is NOT defined).

    As you can see, isset() is weird, and I need to emulate its behaviour.

    The question is: is it possible to do that in PHP?

    Greetings, Giovanni


  • gosha bine

    #2
    Re: How to emulate isset() ? (is it possible?)

    Giovanni R. wrote:
    Take a look at this code (you can execute it):
    >
    error_reporting (E_ALL);
    >
    function byVal( $v) {}
    function byRef(&$v) {}
    >
    print '<pre>';
    >
    byVal ($first['inexistent_ind ex']); // gives a notice
    var_dump($first ); // gives a notice
    >
    print '<hr />';
    >
    byRef ($second['inexistent_ind ex']); // does NOT give a notice
    var_dump($secon d); // does NOT give a notice
    >
    print '<hr />';
    >
    isset($third); // does NOT give a notice
    var_dump ($third); // gives a notice
    >
    print '</pre>';
    >
    >
    In the $first case, using byVal(), I get *two* notices.
    In the $second case, using byRef(), I get *zero* notice.
    In the $third case, using isset(), I get *one* notice.
    >
    This means that:
    >
    1) byVal() does NOT define the array and raises a notice
    (and var_dump() raises another notice).
    >
    2) byRef() defines the array and does NOT raise notices
    (neither var_dump() raises a notice, since $second is defined).
    >
    3) isset() does NOT define the array and does NOT raise notices
    (but var_dump() raises a notice, since $third is NOT defined).
    >
    As you can see, isset() is weird, and I need to emulate its behaviour.
    >
    The question is: is it possible to do that in PHP?
    >
    Greetings, Giovanni
    >
    >
    Hi

    what exactly is the desired behaviour of that new, emulated isset?

    --
    gosha bine

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

    Comment

    • Giovanni R.

      #3
      Re: How to emulate isset() ? (is it possible?)

      gosha bine <stereofrog@gma il.comwrote:
      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_ind ex']);

      In this way, even if $array['inexistent_ind ex'] isn't defined, I don't
      get a notice. The fact is that wrapper() adds that index to the array
      and sets $array['inexistent_ind ex'] to NULL. :-(

      So I was asking myself how isset() works and if a similar function could
      be developed using PHP or not.

      Giovanni


      Comment

      • gosha bine

        #4
        Re: How to emulate isset() ? (is it possible?)

        Giovanni R. wrote:
        gosha bine <stereofrog@gma il.comwrote:
        >
        >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_ind ex']);
        >
        In this way, even if $array['inexistent_ind ex'] isn't defined, I don't
        get a notice. The fact is that wrapper() adds that index to the array
        and sets $array['inexistent_ind ex'] 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_ind ex']);

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

        wrapper($array, 'inexistent_ind ex');

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



        --
        gosha bine

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

        Comment

        • Rami Elomaa

          #5
          Re: How to emulate isset() ? (is it possible?)

          Giovanni R. kirjoitti:
          As you can see, isset() is weird, and I need to emulate its behaviour.
          Isset can't be emulated, because it's not a function, it's a language
          construct, similar to echo and unset. You can't replace it's
          functionality with a wrapper function, because it's not a function in
          the first place. That's why it's "wierd" I suppose. However it returns a
          value, and can be used as an expression, so the analogy to echo is not
          complete. Echo is even more weird in that sense.

          --
          Rami.Elomaa@gma il.com

          "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
          usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

          Comment

          • Toby A Inkster

            #6
            Re: How to emulate isset() ? (is it possible?)

            Giovanni R. wrote:
            is it possible?
            Not really -- it's a language construct rather than a normal function.

            If you really want to emulate it, you'll probably need to write an
            extension to PHP (in C).

            --
            Toby A Inkster BSc (Hons) ARCS
            [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
            [OS: Linux 2.6.12-12mdksmp, up 95 days, 3:02.]

            Non-Intuitive Surnames

            Comment

            • Giovanni R.

              #7
              Re: How to emulate isset() ? (is it possible?)

              gosha bine <stereofrog@gma il.comwrote:
              I'm afraid that's not possible, Giovanni.
              Never mind, it's not a big problem. :-)
              The usual workarounds are to use '@' operator to suppress notices
              wrapper(@$array['inexistent_ind ex']);
              Hey, when using the @ operator that index is NOT added to the array!
              Good: it's almost the same result of isset(). :-)

              I hadn't tried that code before because, as far as I *knew*, the @
              operator only suppressed notices, while the real problem was that
              byRef() was adding the index to the array, besides raising the notice.

              I'm now asking myself if this is not a bug - an undocumented feature. :)

              Thanks to all for your answer.

              Giovanni

              Comment

              • Umberto Salsi

                #8
                Re: How to emulate isset() ? (is it possible?)

                "Giovanni R." <linfo2003@NOSP AMlibero.itwrot e:
                Here it is how it could be used:
                >
                print wrapper($array['inexistent_ind ex']);
                >
                In this way, even if $array['inexistent_ind ex'] isn't defined, I don't
                get a notice. The fact is that wrapper() adds that index to the array
                and sets $array['inexistent_ind ex'] to NULL. :-(
                This code will say you all about the existence of the $array and its
                element 'inexistent_ind ex', also if this element is NULL:

                if( isset($array) ){
                echo "the array exists\n";
                if( array_key_exist s('inexistent_i ndex', $array) ){
                echo "the element array[inexistent_inde x] exists\n";
                if( $array['inexistent_ind ex'] === NULL ){
                echo "...and it is NULL\n";
                } else {
                echo "...and it is not NULL\n";
                }
                } else {
                echo "the element array[inexistent_inde x] does not exist\n";
                }
                } else {
                echo "the array does not exist\n";
                }

                Regards,
                ___
                /_|_\ Umberto Salsi
                \/_\/ www.icosaedro.it

                Comment

                • Giovanni R.

                  #9
                  Re: How to emulate isset() ? (is it possible?)

                  Umberto Salsi <salsi@icosaedr o.italiawrote:
                  This code will say you all about the existence of the $array and its
                  element 'inexistent_ind ex', also if this element is NULL: ...
                  Umbe', scusa, ma a che mi serve? ;-)

                  Anyway, I used your code and it confirms that: byVal($array['index'])
                  does not create neither the index nor the array and generates the
                  notice; byRef($array['index']) does not generate the notice, but creates
                  the index and the array; byRef(@$array['index']) does not create neither
                  the index nor the array, and does not generate the notice, like isset().

                  Ciao, Giovanni

                  Comment

                  Working...