how can i obtain name of the class instance from its inside

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sinasalek

    how can i obtain name of the class instance from its inside

    class TTest
    {
    function instance_name()
    {
    echo "instance name : $instance_name" ;
    }
    }

    $test=new TTest();
    $text->instance_name( );

    result :
    instance name : test

    i searched the web, but i couldn't find any real way.
    i just found some programming ways.
    1.
    $test=new TTest('test'); //'test' is name of instance
    2.
    $test=new TTest();
    $test->instance_name= 'test';
    3.
    $instance='test ';
    $$instance=new TTest($instance );

    if need somthing like get_class but not for class name, for instance
    name.
    any idea will be appreciate.

  • Malcolm Dew-Jones

    #2
    Re: how can i obtain name of the class instance from its inside

    sinasalek (sinasalek@yaho o.com) wrote:
    : class TTest
    : {
    : function instance_name()
    : {
    : echo "instance name : $instance_name" ;
    : }
    : }

    : $test=new TTest();
    : $text->instance_name( );

    : result :
    : instance name : test

    : i searched the web, but i couldn't find any real way.
    : i just found some programming ways.
    : 1.
    : $test=new TTest('test'); //'test' is name of instance
    : 2.
    : $test=new TTest();
    : $test->instance_name= 'test';
    : 3.
    : $instance='test ';
    : $$instance=new TTest($instance );

    : if need somthing like get_class but not for class name, for instance
    : name.
    : any idea will be appreciate.


    How can an instance of an object have a name? What would it be the name
    of?


    Do you wish to find the name of the variable that "holds" the object?
    What if you have more than one variable that points to the same object,
    what would the name be then?


    --

    This space not for rent.

    Comment

    • Weird-beard

      #3
      Re: how can i obtain name of the class instance from its inside

      You can name the instances using a static variable. The simplest method
      is incrementing the static variable(name variable ? ) on constructor,
      so that you can have a different value on each instance. You can
      implement a naming mechanism too, instead of justy implementing.
      Hope it helps.

      Comment

      • sinasalek

        #4
        Re: how can i obtain name of the class instance from its inside

        many thanks, may i ask you more explain?

        Comment

        • sinasalek

          #5
          Re: how can i obtain name of the class instance from its inside

          i know, but i need this name for some dynamic actions. it's good too if i
          could retrieve list of variables that contain this value. any idea
          thanks.

          Comment

          • sinasalek

            #6
            Re: how can i obtain name of the class instance from its inside

            i found the solution by lilleman help, but i changed that from class to
            function.

            <?php
            /**
            * get name of object by its value, if its name does unique
            *
            * @param object $object
            * @return string
            */
            function get_instance_na me($object)
            {
            $instance = md5(serialize($ object));

            foreach( $GLOBALS as $variable => $value )
            if( md5(serialize($ value)) == $instance )
            return $variable;
            }

            //--(End)-->example
            class Foo
            {
            public function get_name()
            {
            return get_instance_na me($this);
            }
            }

            $foo = new Foo;
            echo $foo->get_name();
            //--(End)-->example
            ?>

            Comment

            Working...