By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,702 Members | 1,726 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,702 IT Pros & Developers. It's quick & easy.

Locating an object

100+
P: 105
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
Share this Question
Share on Google+
6 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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

100+
P: 105
@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
Expert Mod 5K+
P: 8,639
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

100+
P: 105
@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
Expert Mod 5K+
P: 8,639
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

100+
P: 105
@Dormilich
Then perpahps it would be better to approach this problem from an alternative angle.

Thanks for the advice.
Jul 28 '10 #7

Post your reply

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