In PHP using method chaining how would one go about supplying a functional call after the last method being called in the chain?
Also while using the same instance (see below). This would kill the idea of implementing a destructor.
The end result is a return value and functional call of private "insert()" from the defined chain properties (of course) without having to call it publicly, no matter of the order.
Note, if I echo (__toString) the methods together it would retrieve the final generated unique code which is normal behavior of casting a string.
Example below:
class object { private $data; function __construct($name) { // ... some other code stuff } private function fc($num) { // some wicked code here } public function green($num) { $this->data .= fc($num*10); return $this; } public function red($num) { $this->data .= fc($num*25); return $this; } public function blue($num) { $this->data .= fc($num*1); return $this; } // how to get this baby to fire ? private function insert() { // inserting file_put_content('test_code.txt', $this->data); } } $tss = new object('index_elements'); $tss->blue(100)->green(200)->red(100); // chain 1 $tss->green(0)->red(100)->blue(0); // chain 2 $tss->blue(10)->red(80)->blue(10)->green(0); // chain 3 Chain 1, 2, and 3 would generated an unique code given all the values from the methods and supply an action, e.g. automatically inserting in DB or creating a file (used in this example).
As you can see no string setting or casting or echoing is taking place.