note 47719 deleted from language.oop5.visibility by jacques
| From: | jacques@php.net | Date: | Fri, 26 Nov 2004 14:34:42 +0000 |
| Subject: | note 47719 deleted from language.oop5.visibility by jacques | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-81062@lists.php.net to get a copy of this message | ||
Note Submitter: richard dot quadling at bandvulc dot co dot uk
----
One use of the __call method is to be able to receive methods that don't exist and maybe pass
then on to somewhere else?
The following example simulates the a container.
The outer container is a WebPageContainer.
This will hold WebFrames.
These will hold WebDocuments.
These will hold WeForms.
These will hold WebFormElements.
But what happens if a WebFormElement needs to know the name of the WebFrame, or any other container
name?
Hopefully this class (and example usage) will explain.
<?php
abstract class BubbleMethod
{
public $objOwner;
function __call($sMethod, $aParams)
{
// Has the Owner been assigned?
if (isset($this->objOwner))
{
// Build an 'eval' string to call the owner with the requested method and the supplied
parameters.
$sEval = '$mResult = $this->objOwner->$sMethod(';
foreach($aParams as $iKey => $mValue)
{
$sEval .= '$aParams[' . $iKey . '],';
}
$sEval = rtrim($sEval, ',') . ');';
// Call the 'eval' string.
eval($sEval);
// Return the results from the 'eval' string.
return $mResult;
}
else
{
echo 'Owner for ' . get_class($this) . ' not assigned.';
}
}
}
class A_WebPageContainer
{
private $sName;
public function __construct($sName)
{
$this->sName = $sName;
}
public function GetWebPageContainerName()
{
return $this->sName;
}
}
class A_WebFrame extends BubbleMethod
{
private $sName;
public function __construct($sName)
{
$this->sName = $sName;
}
public function GetWebFrameName()
{
return $this->sName;
}
}
class A_WebDocument extends BubbleMethod
{
private $sName;
public function __construct($sName)
{
$this->sName = $sName;
}
public function GetWebDocumentName()
{
return $this->sName;
}
}
class A_WebForm extends BubbleMethod
{
private $sName;
public function __construct($sName)
{
$this->sName = $sName;
}
public function GetWebFormName()
{
return $this->sName;
}
}
class A_WebFormElement extends BubbleMethod
{
private $sName;
public function __construct($sName)
{
$this->sName = $sName;
}
public function GetWebFormElementName()
{
return $this->sName;
}
}
$objWPC = new A_WebPageContainer('The outer web page container.');
$objWF1 = new A_WebFrame('Frame 1');
$objWF1->objOwner = $objWPC;
$objWF2 = new A_WebFrame('Frame 2');
$objWF2->objOwner = $objWPC;
$objWD1 = new A_WebDocument('Doc 1');
$objWD1->objOwner = $objWF1;
$objWD2 = new A_WebDocument('Doc 2');
$objWD2->objOwner = $objWF2;
$objWFrm1 = new A_WebForm('Form 1');
$objWFrm1->objOwner = $objWD1;
$objWFrm2 = new A_WebForm('Form 2');
$objWFrm2->objOwner = $objWD2;
$objWE1 = new A_WebFormElement('Element 1');
$objWE1->objOwner = $objWFrm1;
$objWE2 = new A_WebFormElement('Element 2');
$objWE2->objOwner = $objWFrm1;
$objWE3 = new A_WebFormElement('Element 3');
$objWE3->objOwner = $objWFrm2;
$objWE4 = new A_WebFormElement('Element 4');
$objWE4->objOwner = $objWFrm2;
echo "The name of the form that '" . $objWE1->GetWebFormElementName() .
"' is in is '" . $objWE1->GetWebFormName() . "'<br />";
echo "The name of the document that '" . $objWE2->GetWebFormElementName() .
"' is in is '" . $objWE2->GetWebDocumentName(). "'<br
/>";
echo "The name of the frame that '" . $objWE3->GetWebFormElementName() .
"' is in is '" . $objWE3->GetWebFrameName(). "'<br />";
echo "The name of the page container that '" . $objWE4->GetWebFormElementName() .
"' is in is '" .$objWE4->GetWebPageContainerName(). "'<br
/>";
?>
The output is ...
The name of the form that 'Element 1' is in is 'Form 1'
The name of the document that 'Element 2' is in is 'Doc 1'
The name of the frame that 'Element 3' is in is 'Frame 2'
The name of the page container that 'Element 4' is in is 'The outer web page
container.'
If anyone has a better "container" I would appreciate a contact!