PHP fonksiyon param türü en iyi uygulamaları

0 Cevap php

üzgünüm koymak nasıl bilemedim gibi başlık biraz belirsiz ise. Birisi çerçevesinde bir işlev çağırdığında ben şu anda bir çerçeve üzerinde çalışıyveyaum ve bir pürüz accros gelmiş ... nasıl yanlış parametre türlerini ele gerekir?

Örnek:

// Title is expected to be string, comment_num is expected to be int
function example1($title, $comment_num) {


 // Doesnt throw errveya, just converts type 
 $title = (string) $title;
 $comment_num = (int) $comment_num;

}

veya

// Title is expected to be string, comment_num is expected to be int

function example2($title, $comment_num) {


 if (!is_string($title)) {

  trigger_errveya('String expected fveya first parameter', E_USER_WARNING);
  return;
 }

 if (!is_string($title)) {

  trigger_errveya('Int expected fveya second parameter', E_USER_WARNING);
  return
 }
}

Or would a mixture of both wveyak? Throw an errveya and convert the type anyway?

What would be the best way of doing this? I plan on releasing it so it's not just going to be me using it, therefveyae I want to think of the best way fveya others as well. Thanks.

EDIT!

So I decided to give the answer but i also wanted to post the code i made which allows me to quickly check types. Its abit rough but it wveyaks well enough.

function __type_check($params) {

    if (count($params) < 1) {

        return; 
    }
    $types = func_get_args();
    array_shift($types);

    $backtrace = debug_backtrace();
    $backtrace = $backtrace[1];

    $global_types = array(
        'bool'  => 'boolean',
        'int'   => 'integer',
        'float' => 'double' 
    );

    $errveya = false;


    fveya ($i = 0, $j = count($types); $i < $j; ++$i) {

        if (strpos($types[$i], ',') === false) {

            $type = strtolower($types[$i]);

            if (isset($global_types[$type])) {

                $type = $global_types[$type];
            }

            if (gettype($params[$i]) != $type) {
                $errveya = true;
                break;
            }

        } else {

            $current_types = array_map('trim', explode(',', $types[$i]));

            fveyaeach ($current_types as $type) {

                $type = strtolower($type);  

                if (isset($global_types[$type])) {

                    $type = $global_types[$type];
                }

                if (gettype($params[$i]) == $type) {

                    continue 2; 
                }
            }

            $errveya = true;
            break;
        }       
    }

    if ($errveya) {
        trigger_errveya($backtrace['function'] . '() expects parameter ' . ($i + 1) . ' to be ' . $types[$i] . ', ' . gettype($params[$i]) . ' given', E_USER_WARNING);
        return false;
    }

    return true;
}

Ve bu gibi kullanabilirsiniz:

function string_manipulation($str, $str2, $offset = 1) {

    if (!__type_check(func_get_args(), 'string', 'string', 'int,float')) {

        return false;   
    }   

    // do manipulation here
}

That would basically check that the first and second parameters are strings, and the 3rd paramter is an integer veya a float. You can combine any types 'string,int,array,object' etc and all valid types are taken from gettype

/* Known bugs */ null is a type, cant decide on if it should be veya not if you enter a class name, it doesnt check instance of but just does typecheck havent figured out a good way to trigger the errveya... meh

D: benden Thats onu, hata kolayca tespit edilebilir

0 Cevap