#40797 [NEW]: empty() breaks when overloading with __get

Ask a Question related to PHP Bugs, Design and Development.

  1. #1

    Default #40797 [NEW]: empty() breaks when overloading with __get

    From: stephan_schmidt at usa dot com
    Operating system: Windows 200X
    PHP version: 5.2.1
    PHP Bug Type: Class/Object related
    Bug description: empty() breaks when overloading with __get

    Description:
    ------------
    When overloading with __get() to grab an index of an associative array
    inside an object, the overloaded variable returns 'false' when tested with
    empty() whether the value is empty or not (see Example 2 for incorrect
    results).

    Testing overloaded variables that were not in an associative array with
    empty() returns the expected results (Example 3).

    Reproduce code:
    ---------------
    <?
    class A {
    public $myArray = array('nonEmptyVar' => 5, 'emptyVar' => '');
    function __get($var) {
    return $this->myArray[$var];
    }
    }

    class B {
    public $nonEmptyVar = 5;
    public $emptyVar = '';
    function __get($var) {
    return $this->$var;
    }
    }

    $a = new A();
    $b = new B();

    echo '<p>Example 1:<br>';
    echo empty($a->myArray['nonEmptyVar']) ? 'empty' : 'not empty';
    echo '<br>';
    echo empty($a->myArray['emptyVar']) ? 'empty' : 'not empty';

    echo '<p>Example 2:<br>';
    var_dump($a->nonEmptyVar);
    echo empty($a->nonEmptyVar) ? 'empty' : 'not empty';
    echo '<br>';
    var_dump($a->emptyVar);
    echo empty($a->emptyVar) ? 'empty' : 'not empty';

    echo '<p>Example 3:<br>';
    echo empty($b->nonEmptyVar) ? 'empty' : 'not empty';
    echo '<br>';
    echo empty($b->emptyVar) ? 'empty' : 'not empty';
    ?>

    Expected result:
    ----------------
    Example 1:
    not empty
    empty

    Example 2:
    int(5) not empty
    string(0) "" empty

    Example 3:
    not empty
    empty

    Actual result:
    --------------
    Example 1:
    not empty
    empty

    Example 2:
    int(5) empty
    string(0) "" empty

    Example 3:
    not empty
    empty

    --
    Edit bug report at [url]http://bugs.php.net/?id=40797&edit=1[/url]
    --
    Try a CVS snapshot (PHP 4.4): [url]http://bugs.php.net/fix.php?id=40797&r=trysnapshot44[/url]
    Try a CVS snapshot (PHP 5.2): [url]http://bugs.php.net/fix.php?id=40797&r=trysnapshot52[/url]
    Try a CVS snapshot (PHP 6.0): [url]http://bugs.php.net/fix.php?id=40797&r=trysnapshot60[/url]
    Fixed in CVS: [url]http://bugs.php.net/fix.php?id=40797&r=fixedcvs[/url]
    Fixed in release: [url]http://bugs.php.net/fix.php?id=40797&r=alreadyfixed[/url]
    Need backtrace: [url]http://bugs.php.net/fix.php?id=40797&r=needtrace[/url]
    Need Reproduce Script: [url]http://bugs.php.net/fix.php?id=40797&r=needscript[/url]
    Try newer version: [url]http://bugs.php.net/fix.php?id=40797&r=oldversion[/url]
    Not developer issue: [url]http://bugs.php.net/fix.php?id=40797&r=support[/url]
    Expected behavior: [url]http://bugs.php.net/fix.php?id=40797&r=notwrong[/url]
    Not enough info: [url]http://bugs.php.net/fix.php?id=40797&r=notenoughinfo[/url]
    Submitted twice: [url]http://bugs.php.net/fix.php?id=40797&r=submittedtwice[/url]
    register_globals: [url]http://bugs.php.net/fix.php?id=40797&r=globals[/url]
    PHP 3 support discontinued: [url]http://bugs.php.net/fix.php?id=40797&r=php3[/url]
    Daylight Savings: [url]http://bugs.php.net/fix.php?id=40797&r=dst[/url]
    IIS Stability: [url]http://bugs.php.net/fix.php?id=40797&r=isapi[/url]
    Install GNU Sed: [url]http://bugs.php.net/fix.php?id=40797&r=gnused[/url]
    Floating point limitations: [url]http://bugs.php.net/fix.php?id=40797&r=float[/url]
    No Zend Extensions: [url]http://bugs.php.net/fix.php?id=40797&r=nozend[/url]
    MySQL Configuration Error: [url]http://bugs.php.net/fix.php?id=40797&r=mysqlcfg[/url]
    stephan_schmidt at usa dot com Guest

  2. Similar Questions and Discussions

    1. #39337 [NEW]: Array creation when using overloading (__get) does not work (NULL)
      From: phpbugs at thequod dot de Operating system: Ubuntu Linux PHP version: 5CVS-2006-11-01 (CVS) PHP Bug Type: Arrays...
    2. #29234 [Com]: empty($object->property) incorrect when property has access overloaded (__get)
      ID: 29234 Comment by: phpbugs at thunder-2000 dot com Reported By: chrissy at codegoat dot com Status: No...
    3. #39337 [Opn]: Array creation when using overloading (__get) does not work (NULL)
      ID: 39337 User updated by: phpbugs at thequod dot de Reported By: phpbugs at thequod dot de Status: Open Bug Type: ...
    4. #24915 [Com]: empty()/isset() misleading with __get/__set
      ID: 24915 Comment by: myle34 at hotmail dot com Reported By: tater at potatoe dot com Status: Open Bug Type: ...
    5. #25885 [NEW]: mail() causes apache2 to crash when message is empty, and headers is non-empty
      From: ben at krackeler dot com Operating system: WinXP PHP version: 4.3.3 PHP Bug Type: Mail related Bug description: ...
  3. #2

    Default #40797 [Opn->Fbk]: empty() breaks when overloading with __get

    ID: 40797
    Updated by: [email]tony2001@php.net[/email]
    Reported By: stephan_schmidt at usa dot com
    -Status: Open
    +Status: Feedback
    Bug Type: Class/Object related
    Operating System: Windows 200X
    PHP Version: 5.2.1
    New Comment:

    That's exactly how it works here.
    Make sure you've disable any zend_extensions.


    Previous Comments:
    ------------------------------------------------------------------------

    [2007-03-13 23:57:17] stephan_schmidt at usa dot com

    Description:
    ------------
    When overloading with __get() to grab an index of an associative array
    inside an object, the overloaded variable returns 'false' when tested
    with empty() whether the value is empty or not (see Example 2 for
    incorrect results).

    Testing overloaded variables that were not in an associative array with
    empty() returns the expected results (Example 3).

    Reproduce code:
    ---------------
    <?
    class A {
    public $myArray = array('nonEmptyVar' => 5, 'emptyVar' => '');
    function __get($var) {
    return $this->myArray[$var];
    }
    }

    class B {
    public $nonEmptyVar = 5;
    public $emptyVar = '';
    function __get($var) {
    return $this->$var;
    }
    }

    $a = new A();
    $b = new B();

    echo '<p>Example 1:<br>';
    echo empty($a->myArray['nonEmptyVar']) ? 'empty' : 'not empty';
    echo '<br>';
    echo empty($a->myArray['emptyVar']) ? 'empty' : 'not empty';

    echo '<p>Example 2:<br>';
    var_dump($a->nonEmptyVar);
    echo empty($a->nonEmptyVar) ? 'empty' : 'not empty';
    echo '<br>';
    var_dump($a->emptyVar);
    echo empty($a->emptyVar) ? 'empty' : 'not empty';

    echo '<p>Example 3:<br>';
    echo empty($b->nonEmptyVar) ? 'empty' : 'not empty';
    echo '<br>';
    echo empty($b->emptyVar) ? 'empty' : 'not empty';
    ?>

    Expected result:
    ----------------
    Example 1:
    not empty
    empty

    Example 2:
    int(5) not empty
    string(0) "" empty

    Example 3:
    not empty
    empty

    Actual result:
    --------------
    Example 1:
    not empty
    empty

    Example 2:
    int(5) empty
    string(0) "" empty

    Example 3:
    not empty
    empty


    ------------------------------------------------------------------------


    --
    Edit this bug report at [url]http://bugs.php.net/?id=40797&edit=1[/url]
    tony2001@php.net Guest

  4. #3

    Default #40797 [Fbk->Opn]: empty() breaks when overloading with __get

    ID: 40797
    User updated by: stephan_schmidt at usa dot com
    Reported By: stephan_schmidt at usa dot com
    -Status: Feedback
    +Status: Open
    Bug Type: Class/Object related
    Operating System: Windows 200X
    PHP Version: 5.2.1
    New Comment:

    The "Actual Results" were from the results of the above script run on a
    clean install of PHP 5.2.1 (Win 2000 SP4). No zend extensions are
    present or in use.

    The code above did not run as expected on any of the Win 200X servers
    it was tested on. Line 6 of the output read "int(5) empty" in every
    test case.

    I suspect it may have to do with the fact that empty() does not accept
    a function call as a parameter (i.e. empty(trim($myVar))) since
    overloading calls __get. However, one would expect it to also result in
    a fatal error if that were the case.


    Previous Comments:
    ------------------------------------------------------------------------

    [2007-03-14 00:11:15] [email]tony2001@php.net[/email]

    That's exactly how it works here.
    Make sure you've disable any zend_extensions.

    ------------------------------------------------------------------------

    [2007-03-13 23:57:17] stephan_schmidt at usa dot com

    Description:
    ------------
    When overloading with __get() to grab an index of an associative array
    inside an object, the overloaded variable returns 'false' when tested
    with empty() whether the value is empty or not (see Example 2 for
    incorrect results).

    Testing overloaded variables that were not in an associative array with
    empty() returns the expected results (Example 3).

    Reproduce code:
    ---------------
    <?
    class A {
    public $myArray = array('nonEmptyVar' => 5, 'emptyVar' => '');
    function __get($var) {
    return $this->myArray[$var];
    }
    }

    class B {
    public $nonEmptyVar = 5;
    public $emptyVar = '';
    function __get($var) {
    return $this->$var;
    }
    }

    $a = new A();
    $b = new B();

    echo '<p>Example 1:<br>';
    echo empty($a->myArray['nonEmptyVar']) ? 'empty' : 'not empty';
    echo '<br>';
    echo empty($a->myArray['emptyVar']) ? 'empty' : 'not empty';

    echo '<p>Example 2:<br>';
    var_dump($a->nonEmptyVar);
    echo empty($a->nonEmptyVar) ? 'empty' : 'not empty';
    echo '<br>';
    var_dump($a->emptyVar);
    echo empty($a->emptyVar) ? 'empty' : 'not empty';

    echo '<p>Example 3:<br>';
    echo empty($b->nonEmptyVar) ? 'empty' : 'not empty';
    echo '<br>';
    echo empty($b->emptyVar) ? 'empty' : 'not empty';
    ?>

    Expected result:
    ----------------
    Example 1:
    not empty
    empty

    Example 2:
    int(5) not empty
    string(0) "" empty

    Example 3:
    not empty
    empty

    Actual result:
    --------------
    Example 1:
    not empty
    empty

    Example 2:
    int(5) empty
    string(0) "" empty

    Example 3:
    not empty
    empty


    ------------------------------------------------------------------------


    --
    Edit this bug report at [url]http://bugs.php.net/?id=40797&edit=1[/url]
    stephan_schmidt at usa dot com Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139