By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,601 Members | 1,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,601 IT Pros & Developers. It's quick & easy.

Calling a subclass's method

P: n/a
Hello

I'm new to OOP in PHP and I have this question:

I have a class called "Form", which contains a collection of classes
(objects) called "Textbox".
Now I need to call the Textbox class's method "getOuterHTML()".

In Visual Basic, you should do it like this:
oForm.getTextBoxByID("Number").getOuterHTML()

In PHP I tried this: $oForm->getTextBoxByID("Number")->getOuterHTML();
But that doesn't work...

Is there a way to do this in one statement? (I use PHP4).
Thanks in advance!
Ramon.
Jun 14 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Rik
Ramon wrote:
Hello

I'm new to OOP in PHP and I have this question:

I have a class called "Form", which contains a collection of classes
(objects) called "Textbox".
Now I need to call the Textbox class's method "getOuterHTML()".

In Visual Basic, you should do it like this:
oForm.getTextBoxByID("Number").getOuterHTML()

In PHP I tried this: $oForm->getTextBoxByID("Number")->getOuterHTML();
But that doesn't work...

Is there a way to do this in one statement? (I use PHP4).


How does you object "contain" the textboxes, and how have you defined the
method getTextBoxByID?

I'm no real OOP programmer, but in this case I'd:

<?php

class B{
function display(){
echo "works";
}
}

class A{
var $bs = array();
function add(){
$this->bs[] = new B;
}
}

$a = new A;
$a->add();

$a->bs[0]->display();

?>

Grtz,
--
Rik Wasmus
Jun 14 '06 #2

P: 5
PHP 4 or 5? 5 supports a syntax similar to what you have above, but I'd need more info. I know that PHP5 doesn't allow calling a subclass method if it's not declared explicitly in th e parent class (usually as abstract, or public...) But that's probably not what you're problem is. PHP4 doesn't allow "linking" together objects like you're doing (but 5 does), and so:

$box = $oForm->getTextBoxByID("Number");
$contents = $box->getOuterHTML();

Does this work?

(Oh - re-read you have PHP 4. Nope, gotta use two statements or upgrade to PHP5)
Jun 14 '06 #3

P: n/a
What you are describing is not a subclass but a contained object. A subclass
inherits from a superclass whereas you are instantiating an object from
within another object. There is a difference.

What you want is this...

$oForm->getTextBoxByID->getOuterHTML("Number");

--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
"Ramon" <it******@zonnet.nl> wrote in message
news:44***********************@news.xs4all.nl...
Hello

I'm new to OOP in PHP and I have this question:

I have a class called "Form", which contains a collection of classes
(objects) called "Textbox".
Now I need to call the Textbox class's method "getOuterHTML()".

In Visual Basic, you should do it like this:
oForm.getTextBoxByID("Number").getOuterHTML()

In PHP I tried this: $oForm->getTextBoxByID("Number")->getOuterHTML();
But that doesn't work...

Is there a way to do this in one statement? (I use PHP4).
Thanks in advance!
Ramon.

Jun 14 '06 #4

P: n/a
Thanks Tony for your explanation,

but I don't think I understand your solution. The getOuterHTML method
expects no parameters. This is the error I get: "Call to a member function
on a non-object". Did you mean that I have to adjust the getOuterHTML method
in any way?

This is my code (in short):

<?

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]);
}
}

$oTxtNumber = new TextBox ("Nr", "Number", "123");
$oForm->addTextBox ($oTxtNumber);

echo $oForm->getTextBoxByID("Nr")->getOuterHTML();

?>
"Tony Marston" <to**@NOSPAM.demon.co.uk> wrote in message
news:e6*******************@news.demon.co.uk...
What you are describing is not a subclass but a contained object. A
subclass inherits from a superclass whereas you are instantiating an
object from within another object. There is a difference.

What you want is this...

$oForm->getTextBoxByID->getOuterHTML("Number");

--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
"Ramon" <it******@zonnet.nl> wrote in message
news:44***********************@news.xs4all.nl...
Hello

I'm new to OOP in PHP and I have this question:

I have a class called "Form", which contains a collection of classes
(objects) called "Textbox".
Now I need to call the Textbox class's method "getOuterHTML()".

In Visual Basic, you should do it like this:
oForm.getTextBoxByID("Number").getOuterHTML()

In PHP I tried this: $oForm->getTextBoxByID("Number")->getOuterHTML();
But that doesn't work...

Is there a way to do this in one statement? (I use PHP4).
Thanks in advance!
Ramon.


Jun 14 '06 #5

P: n/a
Rik
Ramon wrote:
This is my code (in short): function getOuterHTML () {
return "<input type='text' name='" . $this->m_sName . "'
value=' . $this->m_sValue . '>";
{

I assume the { is } in your actual code?

Try this:

<?
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;
}
}

$oTxtNumber = new TextBox ("Nr", "Number", "123");
$oForm = new HTMLForm;
$oForm->addTextBox($oTxtNumber);
echo $oForm->m_oTextBoxes["Nr"]->getOuterHTML();
?>

Works fine in PHP4 here.

Grtz,
--
Rik Wasmus
Jun 14 '06 #6

P: n/a
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.

?>

"Rik" <lu************@hotmail.com> wrote in message
news:58***************************@news1.tudelft.n l...
Ramon wrote:
This is my code (in short):

function getOuterHTML () {
return "<input type='text' name='" . $this->m_sName . "'
value=' . $this->m_sValue . '>";
{

I assume the { is } in your actual code?

Try this:

<?
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;
}
}

$oTxtNumber = new TextBox ("Nr", "Number", "123");
$oForm = new HTMLForm;
$oForm->addTextBox($oTxtNumber);
echo $oForm->m_oTextBoxes["Nr"]->getOuterHTML();
?>

Works fine in PHP4 here.

Grtz,
--
Rik Wasmus

Jun 14 '06 #7

P: n/a
Rik
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..?
In PHP4, it cannot be made private.
An array in an object is just like any other array, so it's the correct way
of adressing it.

Actually, I should have an interface method to call this array:
I've been trying some things, it seems it cannot be done in one line, except
when using constructs like:
call_user_func(array($oForm->getgetTextBoxByID("Nr"), 'outerHTML));

But I'm no real OOP programmer, maybe someone else could enlighten you.
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();


First it does work, now it doesn't work? Works perfectly here.

If it actually doesn't work, try turning on error_reporting and var_dump()
your objects. (For instance, in the example code you forgot to instantiate
the $oForm object).

If you mean it isn't suitable to your needs, perhaps some more detailed
explanation of your exact needs are in order.

Grtz,
--
Rik Wasmus
Jun 14 '06 #8

P: n/a
Ok,

thanks for your efforts Rik.
At least you gave me a way to make it work!

groetjes,
Ramon.

"Rik" <lu************@hotmail.com> wrote in message
news:ec***************************@news1.tudelft.n l...
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..?


In PHP4, it cannot be made private.
An array in an object is just like any other array, so it's the correct
way
of adressing it.

Actually, I should have an interface method to call this array:


I've been trying some things, it seems it cannot be done in one line,
except
when using constructs like:
call_user_func(array($oForm->getgetTextBoxByID("Nr"), 'outerHTML));

But I'm no real OOP programmer, maybe someone else could enlighten you.
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();


First it does work, now it doesn't work? Works perfectly here.

If it actually doesn't work, try turning on error_reporting and var_dump()
your objects. (For instance, in the example code you forgot to instantiate
the $oForm object).

If you mean it isn't suitable to your needs, perhaps some more detailed
explanation of your exact needs are in order.

Grtz,
--
Rik Wasmus

Jun 14 '06 #9

P: n/a
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.
js*******@attglobal.net
==================
Jun 14 '06 #10

P: n/a
Rik
Jerry Stuckle wrote:
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:

<snip code>


That's what I tried, but PHP4 has thsi to say about thes second ->:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or
';'

PHP5 indeed handles this with no problems.

Maybe this has something to do with returning by reference?

Grtz,
--
Rik Wasmus
Jun 14 '06 #11

P: n/a
Rik schrieb:
That's what I tried, but PHP4 has thsi to say about thes second ->:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or
';'

PHP5 indeed handles this with no problems.

Maybe this has something to do with returning by reference?


Indeed PHP4 passes objects by value (thus creates new instances), which
has been changed in PHP5. To pass references in PHP4 use the & character:

// & in function declaration to return reference
function &getTextBoxByID($sID) {
return $this->m_oTextBoxes[$sID];
}

// & before parameter to pass it by reference
// & with = to assign reference
function addTextBox(&$oTextBox) {
$this->m_oTextBoxes[$oTextBox->getID()] =& $oTextBox;
}

HTH
Markus
Jun 14 '06 #12

P: n/a
Rik wrote:
Jerry Stuckle wrote:
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:

<snip code>

That's what I tried, but PHP4 has thsi to say about thes second ->:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or
';'

PHP5 indeed handles this with no problems.

Maybe this has something to do with returning by reference?

Grtz,


Don't know - haven't used PHP 4 for ages. But as Markus pointed out, you can
return the object by reference.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 14 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.