|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-07-19 08:00 UTC] bas at tuxes dot nl
Description:
------------
Operator overloading seems to work perfectly for my class 'Amount', but if one (or two) of the both operator sides is an array element the operation fails and PHP complains:
> Notice: Object of class Amount could not be converted to int
I don't think the exact implementation of the operators in 'Amount' is relevant, considering the output below. But FYI:
> public function __add(Amount $otherAmount){
> return new Amount($this->_value + $otherAmount->_value);
> }
>
> public function __assign_add(Amount $otherAmount){
> $this->_value += $otherAmount->_value;
> return $this;
> }
Reproduce code:
---------------
$amount1 = new Amount(500);
$amount2 = new Amount(750);
$amounts[0] = $amount1;
$amounts[1] = $amount2;
echo "amount1 (array index 0): $amount1 (" . $amounts[0] . ")<br/>";
echo "amount2 (array index 1): $amount2 (" . $amounts[1] . ")<br/><br/>";
echo "direct add: " . ($amount1 + $amount2) . "<br>";
echo "array add: " . ($amounts[0] + $amounts[1]) . "<br>";
echo "direct assign-add: " . ($amount1 += $amount2) . ", amount1 = " . $amount1 . "<br>";
echo "array assign-add: " . ($amounts[0] += $amounts[1]) . ", amounts[0] = " . $amounts[0];
Expected result:
----------------
amount1 (array index 0): ? 500,00 (? 500,00)
amount2 (array index 1): ? 750,00 (? 750,00)
direct add: ? 1.250,00
array add: ? 1.250,00
direct assign-add: ? 1.250,00, amount1 = ? 1.250,00
array assign-add: ? 2.000,00, amount1 = ? 2.000,00
Actual result:
--------------
amount1 (index 0): ? 500,00 (? 500,00)
amount2 (index 1): ? 750,00 (? 750,00)
direct add: ? 1.250,00
array add: ? 1.250,00
direct assign-add: ? 1.250,00, amount1 = ? 1.250,00
Notice: Object of class Amount could not be converted to int in example.php on line 13
Notice: Object of class Amount could not be converted to int in example.php on line 13
array assign-add: 2, amounts[0] = 2
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 15:00:01 2025 UTC |
I am also experiencing the issue, specifically - and only - when the left-hand side of the += is my Object (with the __assign_add() implementation) and the right-hand side is a normal array: <?php $arrObj = new MyArrayLikeObject(); $arrObj2 = new MyArrayLikeObject(); // These all work fine (when *both* are MyArrayLikeObject).. $test = $arrObj + $arrObj2; $arrObj += $arrObj2; // But when one of them is an array it fails.. $test = $arrObj + array('athird' => 'array'); // Segmentation fault $arrObj += array( 'some' => 'example', 'associative' => 'array', ); // PHP Fatal error: Could not convert to int... ?> For reference, my class definition here is: <?php class MyArrayLikeObject implements IteratorAggregate, ArrayAccess, Serializable, Countable { public function __assign_add() { return $this; } private $arrayObject; function __construct() { $this->arrayObject = new ArrayObject(array('test' => 'array')); } /** * Defer to an internal instance of ArrayObject for our array-a-bility. */ public function count() { $args = func_get_args(); return call_user_func_array(array($this->arrayObject, __FUNCTION__), $args); } public function getIterator() { $args = func_get_args(); return call_user_func_array(array($this- >arrayObject, __FUNCTION__), $args); } public function offsetExists($index) { $args = func_get_args(); return call_user_func_array(array($this- >arrayObject, __FUNCTION__), $args); } public function offsetGet($index) { $args = func_get_args(); return call_user_func_array(array($this- >arrayObject, __FUNCTION__), $args); } public function offsetSet($index , $newval) { $args = func_get_args(); return call_user_func_array(array($this->arrayObject, __FUNCTION__), $args); } public function offsetUnset($index) { $args = func_get_args(); return call_user_func_array(array($this- >arrayObject, __FUNCTION__), $args); } public function serialize() { $args = func_get_args(); return call_user_func_array(array($this->arrayObject, __FUNCTION__), $args); } public function unserialize($serialized) { $args = func_get_args(); return call_user_func_array(array($this- >arrayObject, __FUNCTION__), $args); } } A great shame since this is the missing link in being able to spin up an object and truly make it pretend to be an array!