Nasıl bir yöntem gibi nesneler sınıf tanımının bir Array üzerine usort () yapmak?

0 Cevap php
class Contact{      
    public $name;      
    public $bgcolor;      
    public $lgcolor;      
    public $email;
    public $element;

    public function __construct($name, $bgcolor, $lgcolor, $email, $element) 
    {         
        $this->name = $name;         
        $this->bgcolor = $bgcolor;         
        $this->lgcolor = $lgcolor;         
        $this->email = $email;  
        $this->element = $element; 
    } 

    public static function sortByName(Contact $p1, Contact $p2)
    {
        return strcmp($p1->name, $p2->name);
    }
}  


class ContactList implements Iterator, ArrayAccess 
{     
    protected $_label;     
    protected $_contacts = array();      

    public function __construct($label) 
    {         
        $this->_label = $label;     
    }      

    public function getLabel() 
    {         
        return $this->_label;     
    }      

    public function addContact(Contact $contact) 
    {        
        $this->_contacts[] = $contact;     
    }      

    public function current() 
    {         
        return current($this->_contacts);     
    }      

    public function key() 
    {         
        return key($this->_contacts);     
    }      

    public function next() 
    {         
        return next($this->_contacts);
    }      

    public function rewind() 
    {         
        return reset($this->_contacts);     
    }      

    public function valid() 
    {         
        return current($this->_contacts);     
    }      

    public function offsetGet($offset) 
    {         
        return $this->_contacts[$offset];     
    }      

    public function offsetSet($offset, $data) 
    {         
        if (!$data instanceof Contact)             
            throw new InvalidArgumentException('Only Contact objects allowed in a ContactList');          
        if ($offset == '') 
        {            
            $this->_contacts[] = $data;         
        } else 
        {             
            $this->_contacts[$offset] = $data;         
        }     
    }      

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

    public function offsetExists($offset) {        
        return isset($this->_contacts[$offset]);    
    }

    public function sort($attribute = 'name')
    {
        $sortFct = 'sortBy' . ucfirst(strtolower($attribute));
        if (!in_array($sortFct, get_class_methods('Contact')))
        { 
            throw new Exception('contact->sort(): Can\'t sort by ' . $attribute);
        }
        usort($this->contact, 'ContactList::' . $sortFct);
    }

}     



public function Sort($property, $asc=true)
{
    // this is where sorting logic takes place
    $_pd = $this->_contact->getProperty($property);
    if ($_pd == null)
    {
        user_error('Property '.$property.' does not exist in class '.$this->_contact->getName(), E_WARNING);
        return;
    }
    // set sortDescriptor
    ContactList::$sortProperty = $_pd;
    // and apply sorting
    usort($this->_array, array('ContactList', ($asc?'USortAsc':'USortDesc')));
}

function getItems(){
    return $this->_array;
} 

class SortableItem extends ContactList
{
    static public $sortProperty;

    static function USortAsc($a, $b)
    {
        /*@var $_pd ReflectionProperty*/
        /*
        $_pd = self::$sortProperty;
        if ($_pd !== null)
        {
            if ($_pd->getValue($a) === $_pd->getValue($b))
                return 0;
            else
                return (($_pd->getValue($a) < $_pd->getValue($b))?-1:1);
        }
        return 0;
    }

    static function USortDesc($a, $b)
    {
        return -(self::USortAsc($a,$b));
    }

}

Bu yaklaşım PHP Warnings: usort() [function.usort]: bizim programın bazı küçük hatalar test etmek ve düzeltmek için bu yöntemleri ve tanımları açıklama gerektiği gibi ben sonradan sağlayabilir her türlü bana veren tutar.

** $billy parametreleri önceden tanımlanmıştır.

$all -> addContact($billy);

// --> ended up adding each contact manually above

$all->Sort('name',true);
$items = $all->getItems();
foreach($items as $contact)
{
    echo $contact->__toString();
}

$all->sort();

Usort kullanma nedeni yeniden düzenlemek adına göre alfabetik olarak sipariş ama nedense ya fonksiyon karşılaştırma açıkçası ben geçmek gibiydi, bir dizi ya da başka hatalar olması gerektiğini belirten bir etmektir. Herhangi bir yardım büyük ölçüde peşin teşekkürler duyacağız.

0 Cevap