473,382 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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.

Jul 17 '05 #1
5 4942
sinasalek (si*******@yahoo.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.
Jul 17 '05 #2
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.

Jul 17 '05 #3
many thanks, may i ask you more explain?

Jul 17 '05 #4
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.

Jul 17 '05 #5
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_name($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_name($this);
}
}

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

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Steven.Xu | last post by:
Hello everyone! I have a problem about create an instance automatically in C#. Now I have a class: namespace1.namespace11.CClass01. I want to create it's instance in a function named...
6
by: Microsoft | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
5
by: Cyril Gupta | last post by:
Hello, I have a class inside another class. The Scenario is like Car->Engine, where Car is a class with a set of properties and methods and Engine is another class inside it with its own set of...
20
by: Shawnk | last post by:
I would like to get the class INSTANCE name (not type name) of an 'object'. I can get the object (l_obj_ref.GetType()) and then get the (l_obj_typ.Name) for the class name. I there any way of...
3
by: Janos | last post by:
Hi, I am a newbie, learning C#. I try to map the concepts I have learned earlier in other OO languages to the concepts used here in C# . In Smalltalk you have class variable, which is here the...
2
by: Daniel Lipovetsky | last post by:
I would like for an object to "report" to a container object when a new instance is created or deleted. I could have a container object that is called when a new instance is created, as below. ...
7
by: beginner | last post by:
Hi Everyone, I have encountered a small problems. How to call module functions inside class instance functions? For example, calling func1 in func2 resulted in a compiling error. "my module...
3
by: wendallsan | last post by:
Hi All, I've stumped myself writing an app that uses Prototype and a bit of PHP. Here is what I have: I have a custom class named Default_county_init_data that, upon initialization makes...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.