473,395 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Calling a subclass's method

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
12 2262
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
dbunny
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Steven Bethard | last post by:
So when I'm writing a class and I define an __init__ method, I sometimes haven't called object.__init__, e.g.: class C(object): def __init__(self, x): self.x = x instead of class...
1
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using...
3
by: john | last post by:
I have a situation where i have a base class and a sub-class. A null instance of the sub-class is passed into a function. The function needs to create a new instance of the sub-class, but the...
5
by: Keith Patrick | last post by:
Could someone tell me if it's possible (and if so, how) to call an explicitly-implemented interface method from a subclass? I have a class in which I have to explicity implement some methods, but...
4
by: Claire | last post by:
I'm having real brain failure today. I've done this lots of times with constructors but not with virtual methods and the compiler complains because Ive put the :base(foo) after the function...
13
by: TS | last post by:
Say i have a class car with properties: Color, Make, Model, Year, DriverID And a Driver class with properties: DriverID, Name The driverID PRIVATE property is the id of the driver from say a...
31
by: damacy | last post by:
hi, there. i have a problem writing a program which can obtain ip addresses of machines running in the same local network. say, there are 4 machines present in the network; , , and and if i...
3
by: bassman2112 | last post by:
I'm having issues with calling a method defined in a subclass on a superclass object. This program is an exercise using inheritance, with an Employee superclass, Salaried and Hourly classes that...
6
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.