473,396 Members | 1,996 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,396 software developers and data experts.

Locating an object

105 100+
Hi everybody,

I would like to find out the location in the array where I have store an object.

I have the following code as example:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     class test{
  4.  
  5.         public static $object_store    =    array();
  6.  
  7.         public function start_object($class_name){
  8.  
  9.             self::$object_store[$class_name]    =    new $class_name;
  10.             return self::$object_store[$class_name];
  11.         }
  12.  
  13.         public function locate($class_name){
  14.  
  15.             self::$object_store[$class_name]->where_am_i();            
  16.         }
  17.  
  18.     }
  19.  
  20.     class sub{
  21.  
  22.         public function where_am_i(){
  23.  
  24.             ##Determine the location where this instance is stored
  25.             ##and output the location, ie:  self::$object_store['sub']
  26.         }
  27.     }
  28.  
  29.     $a = new test;
  30.     $a->start_object("sub");
  31.     $a->locate("sub");
  32.  
  33. ?>
  34.  
Any insight would be appreciated.
Jul 27 '10 #1
6 1361
Dormilich
8,658 Expert Mod 8TB
what do you mean by location? the index in the Array?

from the programming point-of-view, your test class is potentially unstable. the case may appear, where no where_am_i() method is provided by the stored object. IMO the best way to overcome this is by using interfaces.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. interface Locator
  3. {
  4.     public function where_am_i();
  5. }
  6.  
  7.     class test{
  8.  
  9.         public static $object_store    =    array();
  10.  
  11.         public function start_object($class_name){
  12.             // …
  13.         }
  14.  
  15.         public function locate($class_name){
  16.             $class = self::$object_store[$class_name];
  17.             if ($class instanceof Locator)
  18.             {
  19.                 $class->where_am_i();  
  20.             }          
  21.         }
  22.  
  23.     }
  24.  
  25.     class sub implements Locator{
  26.  
  27.         public function where_am_i(){
  28.             // …
  29.         }
  30.     }
  31.  
  32.  
there might be as well better suited Design Patterns (it looks a bit like a Factory) for this problem.
Jul 28 '10 #2
aktar
105 100+
@Dormilich
Thanks for the reply,
yes I need to find the location in the array where the object is stored.

Do you recommend that interfaces will still solve the problem?
Jul 28 '10 #3
Dormilich
8,658 Expert Mod 8TB
yes I need to find the location in the array where the object is stored.
that’s easy. the index is $class_name. (unless you’re talking about the location in memory, but then PHP is the wrong language)

Do you recommend that interfaces will still solve the problem?
the interface is not there to solve your problem, but to prevent other problems.
Jul 28 '10 #4
aktar
105 100+
@Dormilich
Lol, yes I realise that the index in $class_name,
but this is only as far as the example is concerned.

What I want to know is if its possible to programatically determine exactly which array solt the object sitting in?

In the real problem class, the structure is more like :

Expand|Select|Wrap|Line Numbers
  1. $object_store[$set_reference][$class_name] = Object of $class_name;
So I would need to find out the set reference in order to fetch information from sibling objects.

ps, there will be many of these set_references, all of which are automatically generated.
Jul 28 '10 #5
Dormilich
8,658 Expert Mod 8TB
array solt
didn’t find that in my dictionary.

So I would need to find out the $set_reference in order to fetch information from sibling objects
you would probably have to loop through the multidimensional array. however, there might be some SPL classes that can help you there (but I’m not an SPL expert)
Jul 28 '10 #6
aktar
105 100+
@Dormilich
Then perpahps it would be better to approach this problem from an alternative angle.

Thanks for the advice.
Jul 28 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ney André de Mello Zunino | last post by:
Hello. Given a sorted collection of strings, what would a good (the best?) strategy be to allow fast access to an item, based on a search substring which should match the beginning of the...
3
by: Rene | last post by:
I am currently developing a Widows application that uses remoting to pass objects back and forth from the server to the client computer and vice-versa. When an object is instantiated and loaded on...
0
by: eedych | last post by:
Hi all, I am trying to do a 3-tier application to learn C#. Now I got a WinForm as GUI, a service, and a remoting object "SqlSender" to connect MSDE. I found such interesting thing. See following...
4
by: Remi Hugnon | last post by:
Hi, I want to build an expandable treeview. I saw some code on VB but I know only C# I plan for my class that each nodes has its own id (using span for example). Into a single aspx page...
19
by: Alan Carpenter | last post by:
Access 8 on Win98 and WinXP I'm having trouble with wildcards in the .Filename property of the FileSearch Object giving different results on win98 and XP. I've been successfully using...
0
by: shaurya | last post by:
I need to open an Excel file that has been stored in the Access Database using the insert Object functionality of MSAccess manually. What i am aware of is that i cant just read the field...
3
by: jundavid | last post by:
How would I locate a database object, e.g. table, using VBA? After locating the object how would I add a column or field to the table? Thanks! Dave
2
by: =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?= | last post by:
Hi, I've tried locating some code that can recreate an object from it's string representation... The object in question is really a dictionary containing other dictionaries, lists, unicode...
4
by: Ty | last post by:
Hi all Short version of my problem: i have a Datagrid (Flexgrid from ComponentOne) with a Datatable as source. I need to search a row in the datatable, using a primary key column in the...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.