views:

2311

answers:

4

Is it possible to overload operators in PHP? Specifically I would like to create an Array class and would like to overload the [] operator.

+1  A: 

PHP's concept of overloading and operators (see Overloading, and Array Operators) is not like C++'s concept. I don't believe it is possible to overload operators such as +, -, [], etc.

Possible Solutions

grammar31
Better than the Iterator is the SPL ArrayObject, which I've linked to below. It provides the whole slew of array functionality.
cbeer
A: 

It appears not to be a feature of the language, see this bug. However, it looks like there's a package that lets you do some sort of overloading.

Benson
+9  A: 

If you are using PHP5 (and you should be), take a look at the SPL ArrayObject classes. The documentation isn't too good, but I think if you extend ArrayObject, you'd have your "fake" array.

EDIT: Here's my quick example; I'm afraid I don't have a valuable use case though:

class a extends ArrayObject {
    public function offsetSet($i, $v) {
        echo 'appending ' . $v;
        parent::offsetSet($i, $v);
    }
}

$a = new a;
$a[] = 1;
cbeer
Not only a helpful answer, but the right answer. I use ArrayObjects all the time, and it's an elegant way not only override arrays but to extend the whole object-model and make PHP kick some serious butt.
AlexanderJohannesen
+1  A: 

Put simply, no; and I'd suggest that if you think you need C++-style overloading, you may need to rethink the solution to your problem. Or maybe consider not using PHP.

To paraphrase Jamie Zawinski, "You have a problem and think, 'I know! I'll use operator overloading!' Now you have two problems."

dirtside
-1 answer is simply incorrect, as it IS possible to overload the [] operator. Additionally, its likely that @Chad is not trying to solve a problem with operator overloading, but keeping his code neat and succinct.
Josiah
That's why I said "Put simply, no" rather than "No." I didn't want to explain that you do it by extending certain classes in weird ways, because 1) operator overloading is a bad enough idea even when the syntax for doing it is clean, and 2) the syntax for doing it in PHP isn't clean.
dirtside
Need to rethink my design? So if I want to do complex arithmetic or extensive date arithmetic, I have to use function calls instead of operators? Yuck.
Longpoke
I submit that date operations do not map cleanly to normal arithmetic operators. You might say, "Well when I use a + sign in a date operation, it doesn't mean addition, it means something similar but subtly different," to which I would say that using a commonly-understood operator for something different than what that operator is used for is going to primarily lead to confusion and sorrow.
dirtside
@dirtside By that logic, we should have different operations for Integers and Floats. (Some languages, like OCaml IIRC, do have this, e.g. + for integer addition and +. for float addition.) My guess is you'd say that's a pain in the neck, but it simply shows there's a trade-off you're making there too, and different people simply do it to different degrees. Get off your high horse of one-true-wayism :-) Using the same operator for different purposes isn't heresy, it's practical.(Parentheses for argument lists *and* operator precedence?! That way lies MADNESS!)
agnoster
@agnoster: The issue is *context*. Parentheses for argument lists and operator precedence are easily distinguished: Is there a function name at the beginning? But with using + for integer and date operations, there's no guaranteed way to tell by looking at the operands what's going to happen to the data. You have to look elsewhere to find out what data types the operands are. Now that's not exactly onerous, but it is an extra step that can lead to misunderstanding. Integers and floats behave nearly identically under addition, so that's not a great counterexample.
dirtside