Değişken argüman işlevine iyi bir çözüm?

0 Cevap

Bu şimdi ne var:

 function getAlbum(){
     $args = func_get_args();

     if(!(count($args) % 2) || count($args) == 1) {
         trigger_error('Argument count must be an odd number and greater than one.');
         return false;
     }

     $q = new Query("album_manager");
     $q->select($args[0]);
     unset($args[0]);
     call_user_func_array(array($q, "where"),$args);
     $q->limit(1);
     return send_back_as_array($q);
 }

If you call getAlbum('*','cid',1) you get all attributes where cid=1
If you call getAlbum('cid, anchor', 'cid',1) you get the anchor and cid where cid=1

Birinin kod hata ayıklama olduğunda, onlar bir argüman ayırıcı için cid ve anchor arasındaki , hata ve karışık olsun diye endişe duyuyorum dışında, çalışıyor .

Herkes bir daha zarif bir çözüm var mı? Ben sadece ilgileniyorum.

Ben getAlbum('cid','anchor','where:','cid',1) kabul ettiği için değişen düşündüm ama bu daha iyi olacak emin değilim.

Bu benim için değişti budur:

function getAlbum(){
    // argument one is a comma separated value or an array of what is requested.
    // the rest of the args must be an even number (odd number of args overall)
    // ever even argument is a selector and every odd is a condition 'selector = condition' in query
    // Examples:
    //      getAlbum('*','cid',1);   <-  Select all attributes from album where cid = 1
    //      getAlbum('cid','anchor','test','name','This is a test'); <- select cid from album where anchor = 'test' and 'name' = 'This is a test'
    //      getAlbum(array('cid','anchor'),'test','name'); select cid and anchor where 'test'='name'
    //      getAlbum('cid, anchor','cid',1); select cid and anchor where cid=1
    $args = func_get_args();
    if(!(count($args) % 2) || count($args) == 1) {
        trigger_error('Argument count must be an odd number and greater than one.');
        return false;
    }
    $q = new Query("album_manager");
    $q->select((is_array($args[0]) ? implode(", ",$args[0]) : $args[0]));
    unset($args[0]);
    call_user_func_array(array($q, "where"),$args);
    $q->limit(1);
    $array = send_back_as_array($q);
    if(count($array) != 1) return false;
    else return $array;
}

0 Cevap