SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Oct 2006
    Queensland, Australia
    0 Post(s)
    0 Thread(s)

    Specifying Allowed Data Types for Function Arguments

    As far as I know, PHP doesn't have any support for specifying required data types for function arguments. Does anyone know if the PHP developers are looking to add this functionality?

    Most of you would have noticed, that all built-in PHP functions seem to support this, as they all report warning when an incorrect data type is passed for a certain argument. So why don't PHP consider this important for user functions, when they obviously consider this important for PHP's built-in functions?

    In addition to that, PHP makes it difficult to make your own implementation of this, but limiting the trigger_error() function to simply two arguments. It would be handy if trigger_error() allowed the user (programmer) to pass through the file and line information manually, so we had more flexibility in defining our own error-type handlers.

    For example, I've been working on a function that checks the arguments passed to a user-defined PHP function. It looks something like this...

    PHP Code:
    function validate_arguments()
    $return true;
    $parent debug_backtrace();
    $parent $parent[1];

    func_get_args() as $num => $types)
    $types explode(' '$types);

    in_array($arg_type gettype($parent_function['args'][$num]), $types))
    $types_string '';
    $num_types count($types);
    $i 0$i $num_types$i++)
    $i == 0)
    $types_string .= $types[$i];
                    else if(
    $i === $num_types 1)
    $types_string .= ' or '.$types[$i];
    $types_string .= ', '.$types[$i];
    trigger_error($parent['function']."() expects argument $num to be $types_string$arg_type given"E_USER_WARNING$parent['file'], $parent['line']);
    $return false;


    Note how it passes $file and $line to trigger_error(). This is obviously not supported, but you can imagine how nice this function would be if $file and $line argument were supported. Here's an example of what you'd need to put into all of your functions for this to work...

    PHP Code:

    function test_function ($arg1$arg2)
    validate_arguments('string integer''array');

    // Do something here

    Of course, there are little workarounds you could use, but none of them would be quite the same, or as simple to use.

    Now, if PHP had native support for this, then that would be even better. Image how nice it would be to just go...

    PHP Code:
    function test (string $arg1string integer $arg2) {} 
    I'm curious to hear what others have to say in regards to this.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    0 Post(s)
    0 Thread(s)

    This also recently showed up

    I'm not so sure I really want this stuff though.


Posting Permissions

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