From: Operating system: Any PHP version: Irrelevant Package: Scripting Engine problem Bug Type: Feature/Change Request Bug description:Context call feature
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. -- Edit bug report at https://bugs.php.net/bug.php?id=60372&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=60372&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=60372&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=60372&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=60372&r=fixed Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=60372&r=needdocs Fixed in release: https://bugs.php.net/fix.php?id=60372&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=60372&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=60372&r=needscript Try newer version: https://bugs.php.net/fix.php?id=60372&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=60372&r=support Expected behavior: https://bugs.php.net/fix.php?id=60372&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=60372&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=60372&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=60372&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=60372&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=60372&r=dst IIS Stability: https://bugs.php.net/fix.php?id=60372&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=60372&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=60372&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=60372&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=60372&r=mysqlcfg