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

how can method of a parent get name of what subclass its being called from?

P: n/a
I'm very unhappy with the error message that I'm giving in this
method:


/**
* 11-23-03 - getter
*
* We want to run the query against a MySql database and get back a
resource pointer to the retrieved dataset
*
* returns resource pointer (to dataset)
*/
function getInfo() {
if ($this->query) {
$dsResultPointer = $this->datastoreConnector->query($this->query);
if (is_resource($dsResultPointer)) {
return $dsResultPointer;
} else {
$this->resultsObject->addToErrorResults("In getInfo(), in the
class McQueryObject, when we ran a query against the database we
expected to get back a resouce pointer to the retrieved dataset but we
did not.");
}
} else {
$this->resultsObject->addToErrorResults("In getInfo(), in the class
McQueryObject, the query could not be run because there was no query.
The most likely reason for this is that the programmer forgot to call
setInfoToBeSought().");
}
}


The truth is, it will never, ever be called from McQueryObject. It
will always be called from a subclass, and the name of that subclass
is the only thing that would make this error message useful. I looked
at all the class methods listed here:

http://de3.php.net/manual/en/function.get-class.php

But it doesn't seem like there is a method for me to find out from
what class a method is being called.
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
is_a()

use this to test against all the subclasses you know of.
Jul 17 '05 #2

P: n/a
Terence <tk******@fastmail.fm> wrote in message news:<3fc2ae4a$1@herald>...
is_a()

use this to test against all the subclasses you know of.

That's an interesting idea, but I can't assume I'll know what
subclasses there are. This software is open to additions from 3rd
party developers.
Jul 17 '05 #3

P: n/a
Use debug_backtrace(). The following will print out "child_class":

class parent_class {
function Hello() {
list($subclass_info) = debug_backtrace();
$subclass = $subclass_info['class'];
echo "$subclass";
}
}

class child_class extends parent_class {
}

$obj = new child_class();
$obj->Hello();
lk******@geocities.com (lawrence) wrote in message news:<da**************************@posting.google. com>...
I'm very unhappy with the error message that I'm giving in this
method:


/**
* 11-23-03 - getter
*
* We want to run the query against a MySql database and get back a
resource pointer to the retrieved dataset
*
* returns resource pointer (to dataset)
*/
function getInfo() {
if ($this->query) {
$dsResultPointer = $this->datastoreConnector->query($this->query);
if (is_resource($dsResultPointer)) {
return $dsResultPointer;
} else {
$this->resultsObject->addToErrorResults("In getInfo(), in the
class McQueryObject, when we ran a query against the database we
expected to get back a resouce pointer to the retrieved dataset but we
did not.");
}
} else {
$this->resultsObject->addToErrorResults("In getInfo(), in the class
McQueryObject, the query could not be run because there was no query.
The most likely reason for this is that the programmer forgot to call
setInfoToBeSought().");
}
}


The truth is, it will never, ever be called from McQueryObject. It
will always be called from a subclass, and the name of that subclass
is the only thing that would make this error message useful. I looked
at all the class methods listed here:

http://de3.php.net/manual/en/function.get-class.php

But it doesn't seem like there is a method for me to find out from
what class a method is being called.

Jul 17 '05 #4

P: n/a
ch***********@hotmail.com (Chung Leong) wrote in message news:<a1*************************@posting.google.c om>...
Use debug_backtrace(). The following will print out "child_class":

class parent_class {
function Hello() {
list($subclass_info) = debug_backtrace();
$subclass = $subclass_info['class'];
echo "$subclass";
}
}

class child_class extends parent_class {
}

$obj = new child_class();
$obj->Hello();


That's elegant. Thanks much.
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.