Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:00:30+00:00 2026-06-14T06:00:30+00:00

Some languages allow us to name parameters when passing them to methods, e.g.: private

  • 0

Some languages allow us to name parameters when passing them to methods, e.g.:

private static function hello(a, long, list = 0, of = 0, parameters = 0)
{
    ...
}

self::hello(a=1, long=2, list=3, of=4);

It certainly simplifies reading code in some cases. Is there a similar functionality in php?

The most simple construction that I could imagine is:

self::hello(/*a*/ 1, /*long*/ 2, /*list*/ 3, /*of*/ 4);

[offtopic]Love Obj C… It requires to name parameters:[self helloA:1 long:2 list:3 of:4];[/offtopic]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:00:31+00:00Added an answer on June 14, 2026 at 6:00 am

    PHP does not provide such functionality but it can be faked to a large degree of realism. I just wrote this thing. Not 100% tested but if used correctly, it will work.

    Example of how you use it:

    call_user_func_args(function($a1, $a2, array $a3 = null, $a4 = null){
        var_dump(func_get_args());
    }, array(
        'a1'    => 1,
        'a3'    => null,
        'a2'    => 2,
    ));
    // Output
    array (size=3)
      0 => int 1
      1 => int 2
      2 => null
    

    It uses \ in front of classes as it’s namespaced code intended for use with PHP 5.3 or later. Remove the \ to try to use it in 5.2, but no guarantees.

    Here is the code:

    /**
    * Calls a function with named arguments.
    * Just written and quite tested. If you find bugs, please provide feedback and I'll update the code.
    * In a sane usage scenario, it will work. If you try your best, you might break it :)
    * If true, $ValidateInput tries to warn you of issues with your Arguments, bad types, nulls where they should not be.
    * 
    * @copyright Claudrian
    * @param callable $Callable
    * @param array $Arguments
    * @param bool $ValidateInput
    * @return mixed
    */
    function call_user_func_args($Callable, array $Arguments, $ValidateInput = false){
        // Make sure the $Callable is callable
        if(!is_callable($Callable)){
            trigger_error('$Callable is not a callable.', E_USER_WARNING);
            return false;
        }
    
        // No arguments, no game
        if(empty($Arguments)){
            return call_user_func($Callable);
        }
    
        // Validate the input $Arguments
        array_change_key_case($Arguments, CASE_LOWER);
        foreach($Arguments as $ArgumentName => $ArgumentValue){
            if(empty($ArgumentName) or is_numeric($ArgumentName)){
                trigger_error('$Arguments cannot have numeric offsets.', E_USER_WARNING);
                return false;
            }
            if(!preg_match('~^[a-z_][a-z0-9_]*$~', $ArgumentName)){
                trigger_error('$Arguments contains illegal character offsets.', E_USER_WARNING);
                return false;
            }
        }
    
        // Get access to the function
        try {
            $Reflector = new \ReflectionFunction($Callable);
        } catch(\Exception $Exception){
            trigger_error($Exception->getMessage(), E_USER_WARNING);
            return false;
        }
    
        // If function has not arguments, just call it but it's stupid
        $RequiredParameterCount = $Reflector->getNumberOfRequiredParameters();
        $ParameterCount = $Reflector->getNumberOfParameters();
        if(!$ParameterCount){
            return call_user_func($Callable);
        }
    
        // Prepare the $Parameters
        $Parameters = array();
        $PresetParameters = array();
        foreach($Reflector->getParameters() as $Parameter){
            $LowerName = strtolower($Name = $Parameter->getName());
            $Argument = ($Available = array_key_exists($Name, $Arguments)) ? $Arguments[$Name] : null;
            $Default = ($IsDefault = $Parameter->isDefaultValueAvailable()) ? $Parameter->getDefaultValue() : null;
            $Parameters[$LowerName] = array(
                'Name'              => $Name,
                'Offset'            => $Parameter->getPosition(),
                'Optional'          => $Parameter->isOptional(),
                'Nullable'          => $Parameter->allowsNull(),
                'Reference'         => $Parameter->isPassedByReference(),
                'Array'             => $Parameter->isArray(),
                'Defaultable'       => $IsDefault,
                'Default'           => $Default,
                'Available'         => $Available,
                'Provided'          => $Available ? $Argument : $Default,
            );
        }
    
        // Pop pointless nulls (from the last to the first)
        end($Parameters);
        while($Parameter = current($Parameters)){
            if(!$Parameter['Nullable'] or !$Parameter['Optional'] or !is_null($Parameter['Provided'])){
                break;
            }
            array_pop($Parameters); // Pop trailing null optional nullable arguments
            prev($Parameters); // Move one back
        }
    
        // Prepare the final $Arguments
        $Arguments = array();
        foreach($Parameters as $Name => $Parameter){
            if($ValidateInput){
                if(is_null($Parameter['Provided']) and !$Parameter['Nullable']){
                    trigger_error("Argument '{$Name}' does not accept NULL.", E_USER_NOTICE);
                }
                if($Parameter['Array'] and !is_array($Parameter['Provided'])){
                    if(!$Parameter['Nullable'] and is_null($Parameter['Provided'])){
                        trigger_error("Argument '{$Name}' should be an array.", E_USER_NOTICE);
                    }
                }
                if(!$Parameter['Available'] and !$Parameter['Optional'] and !$Parameter['Defaultable']){
                    trigger_error("Argument '{$Name}' is not optional and not provided.", E_USER_NOTICE);
                }
            }
            // Stoe this in the final $Arguments array
            $Arguments[] = $Parameter['Provided'];
        }
        // Invoke the actual function
        return $Reflector->invokeArgs($Arguments);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know some languages allow this. Is it possible in C++?
Pure functional programming languages do not allow mutable data, but some computations are more
In some languages where you cannot override the () operator, I have seen methods
I need to create one drop down list contains some Languages. By selecting any
Some programming languages (like haskell ) allow cyclic dependencies between modules. Since the compiler
In some languages, like php, you don't need to manually initialize each dimension of
When I'm learning Clojure of some languages, the readability and convenience of syntax is
I've been wondering why do some languages, such as Java run on Both Linux
I have heard of types being referred to as boxed in some languages. In
I know that some other languages, such as PHP , support a concept of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.