Fligex Posted July 7, 2015 Share Posted July 7, 2015 I am curious as to how one might go about overloading the Arithmetic Operators for a class. I have a class in which I find myself in need of doing this. Any ideas? Thanks. Link to comment https://forums.phpfreaks.com/topic/297205-how-does-one-overload-arithmetic-operators-for-a-class/ Share on other sites More sharing options...
ignace Posted July 7, 2015 Share Posted July 7, 2015 PHP does not support arithmetic operator overloading. You can however use a simple method to achieve this. class A { public $value; function add(self $b) { $this->value += $b->value; } } Link to comment https://forums.phpfreaks.com/topic/297205-how-does-one-overload-arithmetic-operators-for-a-class/#findComment-1515746 Share on other sites More sharing options...
Fligex Posted July 7, 2015 Author Share Posted July 7, 2015 Hi, thanks for reply. What is the keyword "self" doing? First time I've seen it. Link to comment https://forums.phpfreaks.com/topic/297205-how-does-one-overload-arithmetic-operators-for-a-class/#findComment-1515748 Share on other sites More sharing options...
ignace Posted July 8, 2015 Share Posted July 8, 2015 self points to the current class, so basically it says function add(A $b). You'll see also other like static. However this won't work, since static is late bind, while self is early bind which is why this works. Link to comment https://forums.phpfreaks.com/topic/297205-how-does-one-overload-arithmetic-operators-for-a-class/#findComment-1515802 Share on other sites More sharing options...
Fligex Posted July 8, 2015 Author Share Posted July 8, 2015 Thanks for the extra info ignace, I had guessed self was referencing the current class some how but I'm curious what it is actually doing.. "granted I know this syntax is wrong" but I imagine its something like this: function add(new thisClass($b)) Is that an accurate depiction of what is happening? Btw I didn't end up using the keyword since I wasn't sure what it was doing and I wanted to include more parameters. Link to comment https://forums.phpfreaks.com/topic/297205-how-does-one-overload-arithmetic-operators-for-a-class/#findComment-1515871 Share on other sites More sharing options...
scootstah Posted July 8, 2015 Share Posted July 8, 2015 Not quite. ignace is using type hinting, which means that only a value of the specified type can be passed to that method. In this case self resolves to the class name of which it is used in, or, A. So he is saying that only a value of type A can be passed to that method. Link to comment https://forums.phpfreaks.com/topic/297205-how-does-one-overload-arithmetic-operators-for-a-class/#findComment-1515873 Share on other sites More sharing options...
ignace Posted July 11, 2015 Share Posted July 11, 2015 Like scootsah already pointed out, you can only pass instances of A. That said to make it more natural to arithmetic operations you can use value objects. class Operand { private $value; public function __construct($val) { $this->value = $val; } public function add(self $other) { return new static($this->value + $other->value); } public function __toString() { return $this->value; } }So your program can look like this: $seven = new Operand(7); $four = new Operand(4); $eleven = $seven->add($four); var_dump($seven, $four, $eleven); $twentytwo = $eleven->add($seven)->add($four); var_dump($seven, $four, $eleven, $twentytwo);As you can see $seven remains the value 7 as does $four remain the value 4 throughout the application. Link to comment https://forums.phpfreaks.com/topic/297205-how-does-one-overload-arithmetic-operators-for-a-class/#findComment-1516100 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.