php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #57752 __assign_add doesn't work on arrays?
Submitted: 2007-07-19 08:00 UTC Modified: 2017-10-24 23:02 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: bas at tuxes dot nl Assigned:
Status: Suspended Package: operator (PECL)
PHP Version: 5.2.0 OS: Linux (Debian Etch)
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-15 22:16 UTC] james dot silver at computerminds dot co dot uk
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!
 [2017-10-24 23:02 UTC] kalle@php.net
-Status: Open +Status: Suspended
 [2017-10-24 23:02 UTC] kalle@php.net
The operator PECL package have not had a release for 4 years and development activity seems to have ceased, if a new maintainer picks up this package, then this report should be re-opened
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 09:01:28 2024 UTC