SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)

    Using empty() on arrayaccess instance

    What interface if any do I need to implement to have a instance of arrayaccess function correctly when using empty? The below code is saying the arrayaccess instance is not empty, when I would expect it should be.

    PHP Code:
    <?php
    require_once('active_record_basic_iterator.class.php');
    class 
    ActiveRecordCollection implements arrayaccess,IteratorAggregate,Countable {

        protected 
    $container;
        
        public function 
    __construct() {
        
            
    $this->container = array();
            
            
    $args func_get_args();
            
            foreach(
    $args as $record) {
            
                if(
    $record instanceof IActiveRecordDataEntity) {
                
                    
    $this->container[] = $record;
                
                }
            
            }
        
        }
        
        public function 
    offsetSet($offset,$value) {
            
            if(empty(
    $offset)) {
                
    $offset count($this->container);
                
    $this->container[$offset] = $value;
            } else {
                
    $this->container[$offset] = $value;
            }
        
        }
        
        public function 
    offsetExists($offset) {
        
            return isset(
    $this->container[$offset]);
        
        }
        
        public function 
    offsetUnset($offset) {
        
            unset(
    $this->container[$offset]);
        
        }
        
        public function 
    offsetGet($offset) {
            
            return isset(
    $this->container[$offset]) ? $this->container[$offset] : null;
        
        }
        
        public function 
    getIterator() {
        
            return new 
    ActiveRecordBasicIterator($this->container);
        
        }
        
        public function 
    count() {
        
            return 
    count($this->container);
        
        }

    }
    ?>
    PHP Code:
    $data = new ActiveRecordCollection();
    echo empty(
    $data)?'yes':'no'// no 
    I'm sure this has to do with the object not being empty, but is there any way around this so that the container property is referenced indirectly rather then the object? Is this possible?

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Maybe I'm just uneducated here, but AFAIK, empty() is just a fancy wrapper for "isset, and isn't null or something that evaluates to false". $data is assigned to an object, regardless of what's inside of that object, so it's not empty.

    I think you need to define your own function that has the logic for what you consider "empty".

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    I was hoping to create a somewhat interchangeable object for a array. If its not possible I could always just use count() rather then empty, but that isn't the ideal.

    PHP Code:
    $data = new ActiveRecordCollection();
    echo 
    count($data)==0?'yes':'no'// yes 
    I find it odd there isn't a interface for a empty considering there is one for count. Isset() actually works correctly in that it calls offsetExists().

    PHP Code:
    $data = new ActiveRecordCollection();
    echo isset(
    $data[0])?'yes':'no'// no 


Bookmarks

Posting Permissions

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