|
Posted by Jerry Stuckle on 06/14/06 11:39
Ramon wrote:
> Thanks Rik,
>
> this does work.
> I'm sorry that I keep asking, but I'm trying to understand this.
>
> I wonder if this is the correct way to address the $m_oTextBoxes array.
> That array should be treated as if it were private, I suppose..?
>
> Actually, I should have an interface method to call this array:
>
>
> class HTMLForm {
> var $m_oTextBoxes = array();
>
>
> // Added method: ========================================
>
> function getTextBoxByID($sID) {
> return This->m_oTextBoxes[$sID];
> }
>
> // ======================================================
>
> function addTextBox($oTextBox) {
> $this->m_oTextBoxes[$oTextBox->getID()] = $oTextBox;
> }
>
> }
>
> Then, I still have the question how to call the right instance of the
> TextBox-class's getOuterHTML method.
>
> Because this will not work here: echo
> $oForm->m_oTextBoxes["Nr"]->getOuterHTML();
>
>
> Thanks for your patience!
> Ramon.
>
Ramon,
Yes, you should treat the array as private, even in PHP 4 where you can't
declare it such. One of the purposes of OO is to hide implementation details -
such as your data members.
The following (slightly modified from your previous post) works for me on PHP 5.1.2:
<?
class TextBox {
var $m_sID;
var $m_sName;
var $m_sValue;
// Class Constructor:
function TextBox ($sID, $sName, $sValue) {
$this->m_sID = $sID;
$this->m_sName = $sName;
$this->m_sValue = $sValue;
}
function getID() {
return $this->m_sID;
}
function getOuterHTML () {
return "<input type='text' name='" . $this->m_sName . "' value=' .
$this->m_sValue . '>";
}
}
class HTMLForm {
var $m_oTextBoxes = array();
function addTextBox($oTextBox) {
$this->m_oTextBoxes[$oTextBox->getID()] = $oTextBox;
}
function getTextBoxByID ($sID) {
return ($this->m_oTextBoxes[$sID]);
}
}
$oForm= new HTMLForm();
$oTxtNumber = new TextBox ("Nr", "Number", "123");
$oForm->addTextBox ($oTxtNumber);
echo $oForm->getTextBoxByID("Nr")->getOuterHTML();
?>
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[Back to original message]
|