Is it possible to overload operators in PHP?

PhpOperator Overloading

Php Problem Overview


Specifically, I would like to create an Array class and would like to overload the [] operator.

Php Solutions


Solution 1 - Php

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;

Solution 2 - Php

Actually, the optimal solution is to implement the four methods of the ArrayAccess interface: http://php.net/manual/en/class.arrayaccess.php

If you would also like to use your object in the context of 'foreach', you'd have to implement the 'Iterator' interface: http://www.php.net/manual/en/class.iterator.php

Solution 3 - Php

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

Solution 4 - Php

For a simple and clean solution in PHP 5.0+, you need to implements the ArrayAccess interface and override functions offsetGet, offsetSet, offsetExists and offsetUnset. You can now use the object like an array.

Example (in PHP7+):

<?php
class A implements ArrayAccess {
	private $data = [];

	public function offsetGet($offset) {
		return $this->data[$offset] ?? null;
	}

	public function offsetSet($offset, $value) {
		if ($offset === null) {
			$this->data[] = $value;
		} else {
			$this->data[$offset] = $value;
		}
	}

	public function offsetExists($offset) {
		return isset($this->data[$offset]);
	}

	public function offsetUnset($offset) {
		unset($this->data[$offset]);
	}
}

$obj = new A();
$obj[] = 'a';
$obj['k'] = 'b';
echo $obj[0], $obj['k']; // print "ab"

Solution 5 - Php

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.

Solution 6 - Php

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."

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionchadghView Question on Stackoverflow
Solution 1 - PhpcbeerView Answer on Stackoverflow
Solution 2 - PhppaniczView Answer on Stackoverflow
Solution 3 - Phpgrammar31View Answer on Stackoverflow
Solution 4 - PhpFabien SaView Answer on Stackoverflow
Solution 5 - PhpBensonView Answer on Stackoverflow
Solution 6 - PhpdirtsideView Answer on Stackoverflow