SitePoint Sponsor

User Tag List

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

    Removing Object in a Linked List

    What would be the best way to actually remove the object from the linked list here rather then hide it?

    PHP Code:
    <?php
    class Attribute {

        protected 
    $name;
        protected 
    $value;
        
        protected 
    $attribute;
        
        protected 
    $active true;
        
        public function 
    __construct($name='',$value='') {
        
            
    $this->setName($name);
            
    $this->setValue($value);
        
        }
        
        public function 
    setName($name) {
        
            
    $this->name $name;
        
        }
        
        public function 
    setValue($value) {
        
            
    $this->value $value;
        
        }
        
        public function 
    getName() {
        
            return 
    $this->name;
        
        }
        
        public function 
    getValue() {
        
            return 
    $this->value;
        
        }
        
        public function 
    addAttribute($name,$value) {
        
            if(
    $this->attribute) {
            
                
    $this->attribute->addAttribute($name,$value);
            
            } else {
            
                
    $this->attribute = new Attribute($name,$value);
            
            }
            
            return 
    $this;
        
        }
        
        public function 
    removeAttribute($name) {
        
            if(
    strcasecmp($this->getName(),$name)==0) {
                
    $this->active false;
            }
        
            if(
    $this->attribute) {
            
                
    $this->attribute->removeAttribute($name);
            
            }
            
            return 
    $this;
        
        }
        
        public function 
    render() {
        
            
    $str $this->active?$this->getName().'="'.$this->getValue().'"':'';
            
            if(
    $this->attribute) {
                
                
    $str.= ' '.$this->attribute->render();
                
            }
            
            return 
    $str;
        
        }

    }
    PHP Code:
    $attribute = new Attribute('id','order-form');

    echo

    $attribute

    ->addAttribute('class','form')
    ->
    addAttribute('method','post')
    ->
    addAttribute('action','/index.php')

    ->
    removeAttribute('class')
    ->
    removeAttribute('action')

    ->
    render(); 

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe it's me, but the relationship here doesn't seem right. You have an attribute, which may contain other attributes. But there should be no such relationship(at least, with the usage example provided). Maybe you should have Attribute, and AttributeList or AttributeCollection?

    Since it seems like a list, such a structure seems invalid. I realize you intended this to be a linked list, though.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    Its hypothetical.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about losing the reference to the node to be removed?

    PHP Code:
        public function removeAttribute($name) {
        
            
    // if the next node is our kill target
            
    if($this->attribute && strcasecmp($this->attribute->getName(),$name)==0) {
                
                
    // if theres more than 1 more node
                
    if ($this->attribute->attribute) {
                    
    // overwrite ref to next node, with the "next next" node
                    
    $this->attribute $this->attribute->attribute;
                } else {
                    
    // kill target was the last node
                    
    $this->attribute null;
                }
                
            } else {
                
    // keep searching for it
                
    $this->attribute->removeAttribute($name);
            }
            
            return 
    $this;
        
        } 
    edit - nevermind, that wouldnt work for the first node.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    Yeah, I'm not sure how or if its even possible to remove the first item in the list through its public interface.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could maybe mimic it by doing
    PHP Code:
    $this->setName($this->attribute->getName());
    $this->setValue($this->attribute->getValue());
    $this->attribute $this->attribute->attribute
    unless the first item is the only item in the list. You could maybe have the first item in the list be some type of dummy node that cannot be removed.

    You probably have grander plans, but my little brain keeps thinking numeric indexed array, unset(), array_values() as the internal storage mechanism


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
  •