Home / Tutorials / Named Parameters in PHP

Named Parameters in PHP

Converting a Python Twitter API package to PHP turned out to be more trouble than I initially expected due to the usage of Python’s named parameters. Here is the converted function signature:

In Python a call to that method could look like this:

Because PHP has no ability for users to specify parameters and is instead done entirely based on the definition order, it’s going to look like this:

Everything is wrong with this.

  1. I as a user should not have to know or care what the default values of arguments im not using are.
  2. It is unobvious when/if default values change.
  3. I don’t know what im saying false to without looking back at the declaration.
  4. It looks shitty.

What about arrays?

Whenever I say I’d like to use named parameters, somebody says “why do you need them, just use arrays!”

Well, if it’s my only option I guess I will, but how does it look:

This syntax will make the following syntax available:

Ok so yes, technically this will work, but I lose all ability to docblock anything, type hinting is a chore and I have to do it in EVERY method. There are a lot of methods, which means literally hundreds of lines of boilerplate that could be replaced easily with syntax.

What would it look like in PHP-land? Something like one of these I guess:

Comment on which you prefer and why. Also feel free to suggest other syntax.

Source: philsturgeon.uk

About cmadmin

Web Developer & Designer | Android App Developer

Check Also

Subdomain and Mandomain

Create Dynamic Subdomains using PHP and Htaccess

Dynamic subdomains like Google Blogger and Tumblr.com, I know the vast majority of the general …


How to make your own PHP framework?

Building your own framework can be useful for the following reasons: You can learn a …


Create an Account!
Forgot Password? (close)

Sign Up

Confirm Password
Want to Login? (close)

Forget Password?

Username or Email
%d bloggers like this:
To get latest new / tutorial / technology / development information subscribe with us.
Lets Get Updated with latest trends & tutorials!
Your Information will never be shared with any third party.
Ready for latest tutorials & tools !