PHP – Overloading (Object, Variable, Execution, Methods)
Overloading in PHP, basically to dynamically create properties and methods
I will put some samples on Overloading in Object, Variables, Execution and Methods. There’s few magic method required in Overloading; __set()
, __get()
, __isset()
, __unset()
.
Overloading in Object
class Storage { private $_data = array(); public function __set($name, $value) { $this->_data[$name] = $value; } public function __get($name) { if(isset($this->_data[$name])) { return $this->_data[$name]; } } public function __isset($name) { return isset($this->_data[$name]); } public function __unset($name) { unset($this->_data[$name]); } } $storage = new Storage(); $storage->label = 'new storage'; echo 'Storage label is `' . $storage->label . '`.'; unset($storage->label); echo 'Storage label is ' . (isset($storage->label) ? 'set' : 'not set') . '.'; // Output: // Storage label is `new storage`. // Storage label is not set.
Overloading in Variable
class MultiStorage { private $_text = array(); private $_number = array(); public function __set($name, $value) { if(is_int($value) || is_float($value)) { $this->_number[$name] = $value; } else if(is_string($value)) { $this->_text[$name] = $value; } } public function value($name, $type) { $t = ''; if($type == 'number') $t = '_number'; else if($type == 'text') $t = '_text'; return $this->{$t}[$name]; } public function __isset($name) { return (isset($this->_text[$name]) || isset($this->_number[$name])); } public function __unset($name) { unset($this->_text[$name]); unset($this->_number[$name]); } } $multi_storage = new MultiStorage(); $multi_storage->val = 'new multi storage'; $multi_storage->val = 1; echo 'Multi Storage id is `' . $multi_storage->value('val','number') . '`.'; echo 'Multi Storage label is `' . $multi_storage->value('val','text') . '`.'; unset($multi_storage->val); echo 'Storage id & label is ' . (isset($multi_storage->val) ? 'set' : 'not set') . '.'; // Multi Storage id is `1`. // Multi Storage label is `new multi storage`. // Storage id & label is not set.
Overloading in Execution
class Formatter { private $_data = array(); public function __set($name, $value) { if(is_int($value)) { $new_value = number_format($value,0); } else if(is_float($value)) { $new_value = number_format($value,2); } else if(is_bool($value)) { $new_value = $value ? true : false; } else { $new_value = $value; } $this->_data[$name] = $new_value; } public function __get($name) { if(isset($this->_data[$name])) { return $this->_data[$name]; } } public function __isset($name) { return isset($this->_data[$name]); } public function __unset($name) { unset($this->_data[$name]); } } $formatter = new Formatter(); $formatter->name = 'Abdullah'; $formatter->age = 25; $formatter->money = 1324.232; $formatter->employed = true; echo '<p>Formatter::name -> ' . $formatter->name . '.</p>'; echo '<p>Formatter::age -> ' . $formatter->age . '.</p>'; echo '<p>Formatter::money -> ' . $formatter->money . '.</p>'; echo '<p>Formatter::employed -> ' . $formatter->employed . '.</p>'; // Formatter::name -> Abdullah. // Formatter::age -> 25. // Formatter::money -> 1,324.23. // Formatter::employed -> 1.
Overloading in Methods
There’s differences in Overloading Methods, because we using
__call()
instead of__set()
,__get()
,__isset()
and__unset()
but the concept is similar.
class Math { public function __call($name, $args) { $ret = null; if($name == 'sum') { $ret = 0; if(count($args) > 0) { foreach($args as $k => $v) { if(is_int($v)) { $ret += $v; } } } } else if($name == 'concat') { $ret = implode(';',$args); } else { trigger_error('Invalid method access', E_USER_ERROR); } return $ret; } } $math = new Math(); echo '<h4>Overloading in Methods</h4>'; echo '<p>Summation of 1,2,3,4,5,6 is ' . $math->sum(1,2,3,4,5,6) . '</p>'; echo '<p>Concat of 1,2,3,4,5,6 is ' . $math->concat(1,2,3,4,5,6) . '</p>'; echo '<p>Error of 1,2,3,4,5,6 is ' . $math->blabla(1,2,3,4,5,6) . '</p><hr>'; // Overloading in Methods // Summation of 1,2,3,4,5,6 is 21 // Concat of 1,2,3,4,5,6 is 1;2;3;4;5;6

Hope all these Overloading types will give some ideas, on how to minimizing number of line of codes, reusability of codes, and helps to organize our Objects, Variables, Executions and Methods in proper way. Personally, all these overload types give me more flexibility of design the architecture of the application and how I’m going to write codes in proper manners, and more understandable work flow.