php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36786 empty() on an element of ArrayObject
Submitted: 2006-03-19 11:37 UTC Modified: 2014-03-06 14:03 UTC
From: davd dot nqd at gmail dot com Assigned: helly (profile)
Status: Closed Package: SPL related
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
 [2006-03-19 11:37 UTC] davd dot nqd at gmail dot com
Description:
------------
When applying empty() to an element of an ArrayObject which is the empty string, empty() returns true.  But, the manual for empty() states that "an empty string" is considered empty.

Note: I tried to reproduce this bug using a custom implementation of ArrayAccess which worked as expected.

Reproduce code:
---------------
<?php
$array = array('index' => '');
$arrayObject = new ArrayObject($array);
if (empty($arrayObject['index'])) {
    echo 'empty';
} else {
    echo 'not empty';
} 
?>

Expected result:
----------------
empty

Actual result:
--------------
not empty

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-03-19 11:40 UTC] davd dot nqd at gmail dot com
Oops, I made a typo.

Instead of ".. returns true", I meant "... returns false".
 [2006-03-19 14:54 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Actually the problem is that interface ArrayAccess was not designed to support this. In fact it was designed to only check for existance (neither isset nor empty).

Eventually we could change the interface in PHP 6 or invent some workaround for SPL's ArrayObject/ArrayIterator later.

In case you always need empty you can overwrite offsetExists.
 [2006-03-19 20:31 UTC] davd dot nqd at gmail dot com
Actually, ArrayAccess seems to be able to handle this, the following code will work as expected:
<?php
class demo implements ArrayAccess {
    public $array;
    function __construct()
    {
        $this->array = array('index' => '', 'index2' => 'not empty');
    } 
    function offsetExists($offset)
    {
        if (isset($this->array[$offset])) return true;
        else return false;
    } 

    function offsetGet($offset)
    {
        if ($this->offsetExists($offset)) return $this->array[$offset];
        else return (false);
    } 

    function offsetSet($offset, $value)
    {
        if ($offset) $this->array[$offset] = $value;
        else $this->array[] = $value;
    } 

    function offsetUnset($offset)
    {
        unset ($this->array[$offset]);
    } 
} 

$arrayObject = new demo();
if (empty($arrayObject['index'])) {
    echo '1';
} else {
    echo '0';
} 

if (empty($arrayObject['index2'])) {
    echo '1';
} else {
    echo '0';
} 

?>
 [2014-03-06 14:03 UTC] datibbaw@php.net
-Status: Not a bug +Status: Closed
 [2014-03-06 14:03 UTC] datibbaw@php.net
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at 
http://www.php.net/downloads.php

This seems to have been fixed in 5.2.1, so I'm closing this ticket.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC