syntax I miss in PHP
Here are some syntax additions I’d like to see in PHP:
1. a()()
Whena() returns a callable object (such as a closure) the second set of brackets would call it. That would allow to write some neat code. I am working on a patch for that but it’s a bit harder than I thought.
2.a()[$x]
This is kind of obvious, I wonder why don’t we have it? Should also be able to be an lvalue, though unlessa() returns by-ref or return value is an object it may not do what one wanted.
3.foo(1,2,,4)
Syntax to skip a parameter in a call, which then will be substituted with the default as defined by the function. Would come handy if you have function with many defaults, and you only want to change the last one but leave the rest alone – now you don’t have to look up the actual default’s values.
4.foo(“a” => 1, “c” => 2, “d” => 4)
Named parameters call, which allows you to specify parameters in arbitrary order by name. Would allow to build nice APIs which could accept wider ranges of parameters without resorting to using arrays. That’d also imply call_user_func_array() would accept named arguments.
The problem here might be what to do with unknown arguments – i.e. ones that the function did not expect. I guess the function could just ignore them.
5.$a = [“a”, “b” => “c”];
I’d really like to have short array syntax. Yes, I know it was rejected so many times already, but I still like it.
64 thoughts on “syntax I miss in PHP”
I want to use #1 a()() all the time too.
You know what I would like to have? Correct variable types! int $var, char $var and all the other stuff! You know why? Because the Type Juggling of PHP SUCKS!!!
No it does not, in fact it rules 🙂
Maybe you suck at type casting? 😛
Seriously though, there was a heated debate (on php internals list) on primitive type hinting for function arguments, and given what was said there, don’t expect anything like that in PHP ever 😉 If anything, you can wish for SplTypes extension to be finished.
except 3,I like all others。they are nice and great!
i would love to have all of these features … especially “named parameters” would be a huge benefit for PHP. imo,
Additional to a()(), it would be awesome to have SOME_DEFINE()
Then it would be possible to hook system functions (in theory anyway), like this:
Define('mail','_mail_hook');function _mail_hook($to, $subject, $message, $headers = false, $params = false)
{
$headers .= "From: somename ";
mail($to, $subject, $message, $headers, $params);
}mail(...);
I have a solution that isn’t 100% seamless, but close. We wrote it so we can drop in a 1 line include after the function declaration —
function a($x=1, $y=2, $z=3) { include(‘par.php’);
// code goes here —
}
then use the function like so —
a(_, _ 9);
It uses reflection and some elbow-grease to get it done, but it works.
http://www.seoegghead.com/software/downloads/php-parameter-skipping-and-named-parameters.seo
Nice post, you activated a few minds..
Something I’d really like:
$blog_posts = new blogManager()->getPosts();
This would spare a lot of temporary variables, and avoid a couple of unnecessary Singletons.
You can also use proxy pattern to wrap all requests to an object, but it involves another layer. Also, using __call all your methods must be private or protected, so you can’t implement an interface this way…
It would be cool if there could be another type of variable/method scope in a class (after public,private and protected) called for example ‘secured’ every variable/method declared with secured before access a magic (user defined) function would be called which would check if the access to the variable/method is permitted, this could be very handy ;).
Usage example
class Example { secured $foo; secured $bar; public function __isPermitted($name,$arguments,$caller){ if(//some logic ){ return true; } return false; }}$ex = new Example();$var = $ex->foo;//if isPermitted returns falseFATAL_ERROR: access deniedAh, i forgot… if we had [] array syntax, we wanted JSON-compatible syntax too…
As Rasmus could say… write a C extension ;-P
this would be also nice and posible, for arrays and strings (python style)
//$a = [0,1,2,3,4,5,6,7]
$a[3] == 3
$a[-1] == 7
$a[2:4] == [2, 3]
$a[1:] == [1, 2, 3, 4, 5, 6, 7]
$a[:3] == [0, 1, 2]
$a[::2] == [0, 2, 4, 6] # Only even numbers.
$a[::-1] = [7, 6, 5, 4, 3 , 2, 1, 0] # Reverse order.1, 2, 3 would be so very, very nice to see in PHP. After using 1 & 2 in JS you’re just feeling sad that PHP doesn’t allow for it.
Thanks for some good suggestions 🙂
Regards
Fake1. a()()
I agree with 1, but I also agree that it would lead to a world of pain for people to debug and just generally read. You can do object chaining, which I use every single day. $foo()->bar();2. a()[$x]
YES! This is something I’ve always wanted to have in PHP. Javascript can do it… and I love it. even if you can’t set a value, just retrieving a value is nice.3. foo(1,2,,4)
yeah there’s no need for this… this is how it’s done foo(1,2,null,4). no need to do anything else.4. foo(”a” => 1, “c” => 2, “d” => 4)
This would be pretty cool to have, and if the parameter didn’t exist on the function signature it could just make the variables available for use within the scope of the function. a better syntax would be something like foo(‘a’:1,’c’:2,’d’:4); mostly for readability.5. $a = [“a”, “b” => “c”];
YES! I’d like to have this too, but not that important, array() isn’t too hard to type.Alot of people poopoo’d #4 because it’s kind of hard to read. But, you must remember, PHP doesn’t care about whitespace. so you could write the function call like this:
foo(
”a” => 1,
“c” => 2,
“d” => 4
);foo(1,2,,4) is not good feature to ask although it may seem good idea. I am 100% sure that this feature will get same abused as it got in VBA. Developers will happily call methods with long argument list this way as they have tools that make those calls convenient.
I think named parameters is way better thing to have. 🙂
I’d like the possibility to call a method on the same line as creating the object:
$result = new Object()->theMethod( $a, $b );
The only way I can do this now is with a factory method:
class Object
{
public static function create()
{
return new Object();
}
}$result = Object::create()->theMethod();
Well it’s clear you need to store the object somewhere, otherwise you would just use a static method or function.
But it would make sense if all the mothods allways return the same object:$result = new Object()->populate()->merge( new Object()->populate() );
Pingback:PHP 10.0 Blog: syntax I miss in PHP | Webs Developer
What about
a( {'a'=1, 'b'=2} );
Or even bettera( { $a = 1, $b = 2 } );-> this is in place object generation like in javascript (all properties are public, and can only be atomic)
$options = { $a = 1, $b = 2 };
a( $options );In PHP these objects could also be ArrayObjects ofcourse. Using function calls is always slower then using language constructs (eg: a(array(‘a’=>1,’b’=>2))).
This also holds for
if($a IN [1,2,3]); // Array
if($a IN {$a=1,$b=2}); // Object
if(in_array($a,array(1,2,3)));
if(property_exists($a, new StdClass()));and same with BETWEEN
But arrays are more or less fine as they are in PHP, I would concentrate on the object notation.
Hi,
Yes I like those enhancements. But my favorite is ‘IN’ like Ricardo explained in a previous post.
IN (or even better the superset ‘junctions’) is something you use over and over again when you have the possiblility.1. a()() – Simply a bad programing practice. Not transparent and requires additional comments and jumping throught the code to understand what it actualy does. Hopefuly it wont be implemented.
2. a()[$x] – Prety much the same as above. a()[3]()()[4] looks nice but its just one big mess.
3. foo(1,2,,4) – Function should have as little parameters as possible. 0 is perfect, 1 is nice, 2 is ok, more then 2 means that your coding is really bad and your function does way to many things. You can use single array parameter for such case, where order of keys is not important. functionWithManyParams(array(‘param1’ => 1, ‘param3’ => 3))
4. foo(”a” => 1, “c” => 2, “d” => 4) – Same as above. Use an array as single parameter to a function call. functionWithManyParams(array(‘param1’ => 1, ‘param3’ => 3)).
5. $a = [“a”, “b” => “c”] – Could be useful.
6. if ($i IN (3, 6, 8)) – if (in($i, 3, 6, 8)) – Only single good point i see here is that you could program php code simply by learning MySQL.
7. if ($i BEETWEEN 2 AND 15) – if(beetween($i, 2, 15)) – Same as aboveHopefuly none of above will ever get implemented, php is already enough of a mess.
Your comments regarding 1 and 2 are not right.
Since functions are first-class they can be passed on, they can be wrapped in other functions, they can be stacked etc.
So it is quite valid use case to get a function back and call it immediately. This is good for lazy evaluation, factories etc.a()[3]()()[4] is quite a contrived example isn’t it? You need to work really hard to get into such an situation. And if you are in that situation you might consider applying for a different profession.
With every tool you can make ugly things.I’ve many times come across the situation that I am only interested in the single element of some query result.
$sth = $dbh->prepare("SELECT count(*) FROM fruit");
$sth->execute();
echo $sth->fetchAll()[0];Open your mind for functional programming!! 😀
$sth = $dbh->prepare(“SELECT count(*) FROM fruit”);
$sth->execute();
echo $sth->fetchField();These are pretty fast-and-loose comments.
All of these syntaxes are useful. Some more than others. a()[$x] is probably more useful than a()(). I could do without the latter, but both have irked me a few times.
(3) is implemented by C#, and Microsoft thought about it a lot. Same re: (4). Almost every language except PHP has a short array grammar.
I’ve implemented IN as:
function EQ()
{
$args = func_get_args();
return Utils::inArray($args[0], array_slice($args, 1));
}The same could be done for between. Functions sometimes can make up for a lack of an operator. Sure it’s prefix notation, but whatever 🙂
About points 1,2,3 I see the point, but I guess that someone would then abuse that, I could get a big headache working on something like
1. $a = foo()()()()()()()()();
2. $b = foo()()()()[$x][$y][$z];
3. foo(,,,,1,,,,2,,,,,,,4,,);
Obviously a bad programmer could write awful code using just if…else
if (cond1) if (cond2) if (cond3) else else else foo();
About point 4 and 5 I completely agree and just posted something playing with php 5.3. Hope for something new with php 7 😉
3. foo(1,2,,4) is something i thought about more then one time, would be very handy but maybe not that good for code readability.
Also i like the idea of Zé Ricardo:
$i IN (3, 6, 8 )
but i think it should be still enclosed in a if-statement. MySQL-Syntax is similar. A BETWEEN syntax could be handy too.
if( $i BETWEEN 2 AND 15 )
Okay let’s not get out of hand with the BETWEEN/IN crap. ‘IN’ I can see as actually being useful, though you can replicate it with in_array already. ‘BETWEEN’? Just extra cruft. Type out the extra… 1 character. Or the exact same if you use ‘&&’.
if ($i >= 2 AND $i <= 15)if ($i BETWEEN 2 AND 15)
Just my two cents, though. This isn’t SQL 😉
Would better have it this way:
if (2 <= $i <= 15) {}
Everything is nice. I’d also like to have something like
$i IN (3, 6, 8 )
instead of using
if ($i == 3 || $i == 6 || $i == 8 )
Being able to have an else clause for foreach statements would also be nice:
Everything is nice. I’d also like to have something like
$i IN (3, 6, 8)
instead of using
if ($i == 3 || $i == 6 || $i == 8)
Being able to have an else clause for foreach statements would also be nice:
Am really in need for 1 and 2 too 🙂
I miss something else: the possibility to make functions aliases.
Did you considered using python?
2. I have a patch whipped up for this, the problem is behavior for assignment and then we have references.
function a() { return array('a', 'b', 'c'); }
a()[0] = 'd';Congratulations! You are invented the Python 🙂
I would love to see number 3 and 5 implemented in the near future 🙂
Also:
foo()->bar();
when foo() returns an object with bar() method.
Personally if I had the shortened array syntax I could do without named parameters.
I already use arrays like this quite often but it’s a bit clunky.
you can fake the behavior of 5 by just wrapping it in a json_decode() call. not that cool, but it works.
1,2,3 and 5 would come in handy from time to time, but 4 will lead to lots of unreadable code.
This would certainly be cool. Can’t really imagine that point 5 was rejected.
But I think the named parameter call should be something like this: foo(a=1, c=2, d=4)
The syntax on the list i miss the most is the named parameter call. I would mainly use it to juggle with the parameter order but i also thing it comes with a another catch than you already mentioned.
How is a mix of named and unnamed parameters going to get treated? will the unnamed parameters be handled in the order as they appear in the function definition? Will the order of the parameters in the function call determine the parameter handling? Or will it be up to the programmer to handle the unnamed parameters?
Nice!
I would absolutely love if we got those syntax in next versions… I hate to use Array() constructor when calling a function with array parameter…
4) Why not: foo(array(“a” => 1, “c” => 2, “d” => 4)) ? Sure, not as elegant as what you proposed, but works the same way…
Comments are closed.