Class question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
itbegary
Forum Commoner
Posts: 34
Joined: Sun Jan 05, 2003 2:50 am

Class question

Post by itbegary »

Hello,

I have a question regarding classes. I have a set of classes for doing most of my tasks. They work great. What I also have is a class for logging errors. Each class gets a copy of this error routine. Basically, I declare a single instance and then add it to each active instance of the other classes.

For example, I have a the following classes: players, characters and monsters. Each class has a reference to errorlogger. errorlogger has a single function called logerror($message).

Each class also has a function called "adderrorlogger(&$errorlogger)" to ensure that they are getting a reference fo the errorlogger class.

My question, after that log explanation, is how can I determine which class called the errorlogger->logerror($message) function from with the logerror function.

Basically, I want the logger to determine who called it so it can also log which class through the error. Because it's not inherited I can't use get_parent_class();

The concept goes a little beyond what I have stated above but that's the simplified version.

Any ideas?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Try the __CLASS__ keyword.

I think (can't remember exactly) this is only available from v4.3+ so won't be much use if you have to support earlier versions.

Another option would be to set a default in each class..

Code: Select all

<?php

var $class_name = 'ClassName';

?>
.. and use that.
Last edited by McGruff on Wed Aug 10, 2005 2:52 pm, edited 1 time in total.
itbegary
Forum Commoner
Posts: 34
Joined: Sun Jan 05, 2003 2:50 am

Post by itbegary »

I tried the __CLASS__ keyword but it gives me the active class, which is errorlogger. It would make sense to use something like this but its just not in the cards :). On a single we page I might have a dozen of so classes. What I am really looking for is the ability to not only derive the error but the name of the class the instantiated the error. Here is a little more detail on the heirchy when an error is through.

monsters->database->logging.
characters->database->logging.
characters->skills->database->logging.

So I have an instance of database which is shared between all of the classes. I call a load function in character->skills to load the skills. It throws an error. At best I can pass some information from the database class to the logger (such as the error, sql, some state information, etc). but what I would also really like to pass is the heirchy.

It's impracticle to create a seperate database connection for each object, leaving me with a single database. With a single database object I can't just set a flag that says who is the object that created it.

I do want to say thanks for the feedback though. I know many of times were people have posted something and got nothing... :)
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post by Stoneguard »

you could also change logerror to take a second parm of the class name.

One question. Are you SURE you are carrying only one instance of the class? I ask because if you do the following:

Code: Select all

$errlog = new errorlogger;

$player->errorlog = $errorlog;
it is actually making a copy of the class, doing a pass-by-value.
Post Reply