Edit report at http://bugs.php.net/bug.php?id=49691&edit=1
ID: 49691
Comment by: ziddiri at yahoo dot com
Reported by: generalpd at gmx dot de
Summary: __operator() magic method
Status: Open
Type: Feature/Change Request
Package: Feature/Change Request
Operating System: all
PHP Version: 5.3.0
New Comment:
It is one of most important oo thing that is missing in php. And I share
same thought with you. It should be included in 5.3.x or in php 6.
Previous Comments:
------------------------------------------------------------------------
[2009-09-27 21:05:29] generalpd at gmx dot de
Description:
------------
My suggestion is a new magic method called __operator($op, $rightValue)
or (if that might be the better way) __operator($leftValue, $op)
If defined, a class should be able to handle an operation, like "$classA
+ $classOrValueB". What the method returns is the result of that of that
operation.
Reproduce code:
---------------
// first way
class A1
{
// the method of the instance of $a1
public function __operator($operator, $rightValue)
{
var_dump($rightValue); // 'Hi, All!'
var_dump($operator); // '.'
return '666 . ' . $rightValue;
}
}
// second way
class A2
{
// the method of the instance of $a2
public function __operator($leftValue, $operator)
{
var_dump($leftValue); // 'Hello, World!'
var_dump($operator); // '+'
return '777 + ' . $leftValue;
}
}
// 1st way
$a1 = new A1();
$c1 = $a1 . 'Hi, All!'; // '666 . Hi, All!'
// the result of __operator() method
// 2nd way
$a2 = new A2();
$c2 = 'Hello, World!' + $a2; // '777 + Hello, World!'
// the result of __operator() method
Expected result:
----------------
$c1 = '666 . Hi, All!';
$c2 = '777 + Hello, World!';
Actual result:
--------------
-----
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=49691&edit=1