Req #76939 [NEW]: Arithmetic operations and assignment magic methods

From: Date: Wed, 26 Sep 2018 19:46:05 +0000
Subject: Req #76939 [NEW]: Arithmetic operations and assignment magic methods
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-217251@lists.php.net to get a copy of this message
From: henrique dot borba dot dev at gmail dot com Operating system: PHP version: Next Major Version Package: Math related Bug Type: Feature/Change Request Bug description:Arithmetic operations and assignment magic methods Description: ------------ While developing a NumPy equivalent for PHP (using C) I came across a limitation. I coulnd't perform arithmetic operations with objects, so a code like this is impossible (wich is possible with NumPy): $a = CArray::fromArray($arr); $result = $a + $a; I was thinking about the possibility of a new magic method that would intercept the arithmetic operations: class CArray { ... public function __arithmetic($operator, $operand) { if($operator == PHP_ADD_OP) { return self::add($this, $operand); } ... } } consequently the code below would also be valid if the class were used in conjunction with __offsetGet: $b = CArray::fromArray([]); $a = CArray::fromArray([0, 2, 1, 0]); $b[$a - 1]; Also, assignment magic method would be useful in some situations relative to C pointer index within PHP Objects (like a PHP Object pointing to an C array position in memory using an extension): function test($a) { $b = $a; // POINTS TO 0 $c = $b; // POINTS TO 0 return $c; // POINTS TO 0 } $arr = CArray::fromArray([1, 2]); // UUID: 0 test($arr); // GC RUNS AND FREE $b // $arr is not freed, but its pointer was freed with $b // any operations with $arr will cause memory troubles If $arr has a pointer to a C buffer and contains an __destruct method that free that buffer position, when GC runs the code above it should cause seg faults or memory leaks. During assignment all ($a, $b, $c, $arr) got the same values of $arr (wich contains the [1, 2] array position in memory), instead, it should be capable of "clone" this array to a new memory position and retrieve a new pointer, so $b, $c and $a would all be equal to $arr but in different memory positions. I guess __assign($value) magic method would solve this, by assigning to the object the return of this method, writing over it. Sorry about my bad english, I hope you guys got the idea. -- Edit bug report at https://bugs.php.net/bug.php?id=76939&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=76939&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=76939&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=76939&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=76939&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=76939&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=76939&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=76939&r=needscript Try newer version: https://bugs.php.net/fix.php?id=76939&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=76939&r=support Expected behavior: https://bugs.php.net/fix.php?id=76939&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=76939&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=76939&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=76939&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=76939&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=76939&r=dst IIS Stability: https://bugs.php.net/fix.php?id=76939&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=76939&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=76939&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=76939&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=76939&r=mysqlcfg

« previous php.bugs (#217251) next »