php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60372 Context call feature
Submitted: 2011-11-24 14:50 UTC Modified: 2018-09-18 15:39 UTC
Votes:2
Avg. Score:3.5 ± 1.5
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: ninzya at inbox dot lv Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: Irrelevant OS: Any
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2011-11-24 14:50 UTC] ninzya at inbox dot lv
Description:
------------
I'd like to propose an extension to PHP syntax, that would allow programmer to 
avoid code duplication in some cases and, therefore, would make PHP code more 
readable.

My idea is to extend dynamic call operator ("->method()") add a "context" syntax 
to it's arguments. The syntax would mean that the argument's value is located in 
context of a specific object - the argument is either a property or method. The 
syntax could be as follows:

$obj->area( :$x, :$y);

Note the ":" (colon) character in front of argument. The colon means that the 
argument for the call is a property of object that the invoked method belongs 
to. In this case both arguments are actually "$obj->x" and "$obj->y" values. 
Syntax for method calls is as follows:

$obj->area( :x(), :y());

In this case both arguments are "$obj->x()" and "$obj->x()".

The following more complex example:

$obj->area( :mt_rand( 1, :$x), $y);

Here mt_rand() is a method of $obj (i.e. "$obj->mt_rand()", because colon 
character is preceding the function name)) and since ":mt_rand()" is resolved to 
"$obj->mt_rand()", it's ":$x" argument is resolved to "$obj->x" as well.

However, a call like this:

mt_rand( :$x);

should yield a runtime error because this is not a dynamic call and the argument 
can not be resolved.



Test script:
---------------
class Test {

	public $x =5;
	
	public function y() {
		return 10;
	}

	public function area( $x, $y, $name) {
		echo $name .': ' .$x .' x ' .$y;
	}
	
	public function makeName( $name) {
		return '#' .$name;
	}
}

$test =new Test;
$test->area( :$x, :y(), :makeName( 'test'));

Expected result:
----------------
#test: 5 x 10

Actual result:
--------------
Not implemented yet.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-11-24 14:59 UTC] ninzya at inbox dot lv
An example on how the proposed syntactic sugar would improve code readability.

Before:

if( $user->hasPermission( $user::PERM_EDIT, $user->getProfile())) {
  // has access to edit own profile
}

After:

if( $user->hasPermission( :PERM_EDIT, :getProfile())) {
  // has access to edit own profile
}


Before:
$config->setSettings([
  $config::SOME_TTL =>10,
  $config::SOME_PATH =>"/var/www/...",
  $config::SOMETHING_OTHER =>0xFF
]);

After:
$config->setSettings([
  :SOME_TTL =>10,
  :SOME_PATH =>"/var/www/...",
  :SOMETHING_OTHER =>0xFF
]);

Note that use of ":" inside array definition contextually resolves arguments as 
well. In the last example the ":SOME_TTL" syntax refers to the "SOME_TTL" class 
constant of $config object (i.e. same as "$config::SOME_TTL")
 [2011-11-24 15:06 UTC] ninzya at inbox dot lv
So basically if the feature gets support from the community, I could then prepare 
an RFC describing the proposal in detail.
 [2011-12-30 04:30 UTC] phristen at yahoo dot com
I still hope that PHP will get named parameters one day, and since : is usually used for that, this will create a lot of confusion :)
 [2018-09-18 15:39 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2018-09-18 15:39 UTC] cmb@php.net
> So basically if the feature gets support from the community, I
> could then prepare an RFC describing the proposal in detail.

The feature can't get much support, if it is not discussed on the
internals@ mailing list.  If you're still interested in this
feature, I suggest that you start the RFC process[1].  For the
time being, I'm suspending this ticket.

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue Jul 23 11:01:25 2019 UTC