For example....

I have a class called widget. I create an instance of widget called mywidget.

$mywidget = new widget;

Let's say I want to print a warning message for developers using my class. For example, I want to warn them that they may wanna call one method before calling another. So...in widget, I do something like this....

function dosomething()
{
    if ( $this->notdone )
        echo " You need to do a {this instance}->dosomethingfirst() before calling this method. ";
}

I know how to get the class name...

echo get_class($this)

What I don't know is how to get the instance name. So instead of get_class returning "widget", I need to know how to have php return "mywidget."

Clear as mud?

    I think that I understand what you are saying. But the name is not "mywidget" when you are in that class, in fact it have no name if you don't do a name variable. To my knoledge what you want is not possible in PHP, Java or C#. This is because the instance have no name, only a reference somewhere. "mywidget" is the name of a reference to the instance, not the instance itself.

      Thanks Piranha,

      That makes perfect sense. I had looked around for 1-15 minutes before giving up and coming here. It's not life and death by any means. It would be a nice touch though ("copy and paste {this}"). I'll stick with get_class() for now and the developers hopefully will be smart enough to use the instance variable instead of the class name.

        Perhaps the CLASS and FUNCTION constants might help? Example:

        class widget {
        	function dosomething() {
        		if ( $this->notdone )
        			echo 'error in ' . __CLASS__ . '::' . __FUNCTION__
        			. '(): you must call ' . __CLASS__ . '::dosmethingfirst() first!';
        	}
        }
          bretticus wrote:

          the developers hopefully will be smart enough to use the instance variable instead of the class name.

          The way to think about this problem is that if $mywidget is a widget, and you then write

          $somewidget = $mywidget;

          both $somewidget and $mywidget will refer to the same object - which name should be used in that case?

          In this situation, incidentally, throwing an exception would be more valuable to the developer; then along with your choice of error message they'll have file name, line number, and a stack trace that shows what was being called where at the time. That should help pin down where the wonkiness happened.

            Weedpacket wrote:

            In this situation, incidentally, throwing an exception would be more valuable to the developer; then along with your choice of error message they'll have file name, line number, and a stack trace that shows what was being called where at the time. That should help pin down where the wonkiness happened.

            I agree. However, like I said before. It's really not a significant feature. Basically, I have a security class with an optional feature that uses javascript to hash username+password+salt and not send the password in the clear. I wrote this feature for clients that wanted a password protected site with minimal security need (no credit cards, etc.) but wanted to keep the password secure in case of a man in the middle (I have an algorithm to help prevent session hijacking.) Not a completely bullet-proof solution (mostly because the packets are not encrypted that follow), but as close as I could get without SSL. Anyways, back to that javascript...

            When I first wrote it, I just statically linked the javascript libraries. Later, I thought, "hey, I should dynamically link these scripts, etc." So I wrote a method that just spits out the static html to load the javascript. In the meantime, a developer using it told me that my class was broke. That was some months ago. Then recently I fell victim to my own bad design and struggled for about 15 minutes until I realized I hadn't loaded the javascripts. Now for my point...

            Older login scripts are fine because they are statically linked (html loads javascript libraries.) Just because my method hasn't been called, doesn't mean it's broke. I don't want an error breaking those login pages for various other clients (yeah I'm lazy...I could track them all down.) So now a warning pops up when the login form method gets called without calling the javascript method (again the methods are separate because I initially did not include the javascript as part of the object output.) Instructions state that if the javascript references are already there, just set such-and-such property to true. This is wonky, but the form remains functioning as before.

            I guess I should stop being lazy, track down the older scripts using this class and pull out the static linking. Then I can just include the javascript loading in the login form method (pending the optional property that turns it on is set.)

            Thanks for all your help and suggestions.

              Write a Reply...