Req #49691 [Opn->Bgs]: __operator() magic method

From: Date: Wed, 22 Dec 2010 14:37:44 +0000
Subject: Req #49691 [Opn->Bgs]: __operator() magic method
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-157263@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=49691&edit=1

 ID:                 49691
 Updated by:         johannes@php.net
 Reported by:        generalpd at gmx dot de
 Summary:            __operator() magic method
-Status:             Open
+Status:             Bogus
 Type:               Feature/Change Request
 Package:            Class/Object related
 Operating System:   *
 PHP Version:        *
 Block user comment: N
 Private report:     N

 New Comment:

pecl.php.net/operator exists. but in a weakly typed scripting language
operator overloading easily leads to unmaintainable code.


Previous Comments:
------------------------------------------------------------------------
[2010-03-09 21:42:02] ziddiri at yahoo dot com

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.

------------------------------------------------------------------------
[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


Thread (3 messages)

« previous php.bugs (#157263) next »