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

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
sinasalek
Forum Newbie
Posts: 6
Joined: Tue Jun 29, 2004 4:39 pm

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

Post by sinasalek »

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

Code: Select all

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

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

Code: Select all

result :
instance name : test
i searched the web, but i couldn't find any real way.
i just found some programming ways.
1.

Code: Select all

$test=new TTest('test'); //'test' is name of instance
2.

Code: Select all

$test=new TTest();
 $test->instance_name='test';
3.

Code: Select all

$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.
marike
Forum Newbie
Posts: 24
Joined: Thu Mar 31, 2005 6:19 pm
Location: New York

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

Post by marike »

If your using PHP 5, It looks like you can use the new "Reflection" class to get internal classes. Have a look here.

http://us4.php.net/manual/en/language.o ... ection.php

Looks very cool.

Hope this helps.
Post Reply