473,624 Members | 2,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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) {
$dsResultPointe r = $this->datastoreConne ctor->query($this->query);
if (is_resource($d sResultPointer) ) {
return $dsResultPointe r;
} else {
$this->resultsObjec t->addToErrorResu lts("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->resultsObjec t->addToErrorResu lts("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
setInfoToBeSoug ht().");
}
}


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
4 2797
is_a()

use this to test against all the subclasses you know of.
Jul 17 '05 #2
Terence <tk******@fastm ail.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
Use debug_backtrace (). The following will print out "child_clas s":

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******@geocit ies.com (lawrence) wrote in message news:<da******* *************** ****@posting.go ogle.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) {
$dsResultPointe r = $this->datastoreConne ctor->query($this->query);
if (is_resource($d sResultPointer) ) {
return $dsResultPointe r;
} else {
$this->resultsObjec t->addToErrorResu lts("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->resultsObjec t->addToErrorResu lts("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
setInfoToBeSoug ht().");
}
}


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
ch***********@h otmail.com (Chung Leong) wrote in message news:<a1******* *************** ***@posting.goo gle.com>...
Use debug_backtrace (). The following will print out "child_clas s":

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

Similar topics

14
3803
by: pablo | last post by:
Dear NewsGroupers, I am relatively new to OOP and cannet get my head around this problem. I have two classes. Class Child extends Parent. There is no constructor for the Child class. So when I create a child object the constructor for the parent object is called. That works fine. But now I have the problem that I want to add an already existing Parent object to create a new Child object. How can this be done?
4
7077
by: Ruud de Jong | last post by:
The question I have is: how safe / future proof / portable is the use of the __subclasses__ method that exists for new-style classes? Background: I thought I had found an easy-to-understand application for metaclasses: making classes instantly aware of their subclasses. The context was that I needed a class hierarchy, and I wanted to be able to instantiate subclasses by calling the root class. Which exact subclass would be instantiated...
12
1896
by: Donnal Walter | last post by:
The following method is defined in one of my classes: def setup(self, items={}): """perform setup based on a dictionary of items""" if 'something' in items: value = items # now do something with value if 'another' in items: value = items # do something else with value
4
11987
by: bonono | last post by:
Hi, Suppose my class definition is like this : class A: name = "A" @classmethod def foo(cls): cls.__super.foo()
6
2661
by: BBM | last post by:
I have an object that has a fairly complex construction sequence, so I have written a dedicated "factory" class that invokes the constructor of my object class (which does nothing but instantiate the object and set default blank/null values), and then does all the Db access and number crunching to populate the new object. The factory returns the fully populated object to the caller. All the fields in the object are private, but have...
5
7271
by: RedHair | last post by:
I have a ASP.NET 2.0 web page which inherits a customized base page class and have a master page, their relation is as below Customized base page class --> web page --> master page How to access the property and control of master page from the customized base page class? Btw, I can access the server control of master page from base page class via
12
2281
by: Ramon | last post by:
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()
4
6040
by: many_years_after | last post by:
Hi, pythoners: My wxPython program includes a panel whose parent is a frame. The panel has a button. When I click the button , I want to let the frame destroy. How to implement it? Could the panel invoke the frame's method? Thanks.
6
14868
by: howa | last post by:
Consider example: Animal = function(age) { this.age = age; }; Animal.prototype.sleep = function() { alert("Animal Sleeping..."); };
0
8249
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8493
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2613
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1797
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1493
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.