note 100976 deleted from language.oop5.decon by danbrown
| From: | danbrown@php.net | Date: | Thu, 18 Nov 2010 15:39:23 +0000 |
| Subject: | note 100976 deleted from language.oop5.decon by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174158@lists.php.net to get a copy of this message | ||
Note Submitter: lbergman at considerateconsultancy dot com
----
Multiple constructors can be simulated in a fashion using Inversion of Control. Inversion of Control
means that the callee is deciding how to implement the information provided by the caller. That
greatly simplifies code as the caller does not have to know anything from the callee. They have to
have an agreement about data transfer. That is all.
To accomplish that the caller provides an array of data. What the callee is doing with the data is
not of interest of the caller. That is why the caller is providing the data in a standard format.
Now the callee can decide upon the information in the array how to process the data. An example:
The caller is requesting like this:
$obj = new Template($arVals);
The class Template has this constructor:
function __construct($arVals){
if(is_null($arVals) || !is_array($arVals)){
_conDef();
}
if(count($arVals) == 1){
_conName($arVals[0]);
}else if(count($arVals) == 2){
_conPage2($arVals[0],$arVals[1]);
}else{
_conDef();
}
$this->setName($name);
}
function _conDef(){
$this->setName("base");
$this->setStartPage("templates/base");
}
function __conName($name){
$this->setName($name);
}
function __conPage2($name, $page){
$this->setName($name);
$this->setStartPage($page);
}
How the class Template is handling the information has become independent of the caller.