Locating an object – PHP

  php

Q(Question):

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:


<?php
class test{
public static $object_store = array();
public function start_object($class_name){
self::$object_store[$class_name] = new $class_name;
return self::$object_store[$class_name];
}
public function locate($class_name){
self::$object_store[$class_name]->where_am_i();
}
}
class sub{
public function where_am_i(){
##Determine the location where this instance is stored
##and output the location, ie: self::$object_store['sub']
}
}
$a = new test;
$a->start_object("sub");
$a->locate("sub");
?>

Any insight would be appreciated.

A(Answer):

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.


<?php
interface Locator
{
public function where_am_i();
}
class test{
public static $object_store = array();
public function start_object($class_name){
// …
}
public function locate($class_name){
$class = self::$object_store[$class_name];
if ($class instanceof Locator)
{
$class->where_am_i();
}
}
}
class sub implements Locator{
public function where_am_i(){
// …
}
}

there might be as well better suited Design Patterns (it looks a bit like a Factory) for this problem.

A(Answer):

@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?

A(Answer):

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.

A(Answer):

@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 :

$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.

A(Answer):

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)

A(Answer):

@Dormilich

Then perpahps it would be better to approach this problem from an alternative angle.

Thanks for the advice.

LEAVE A COMMENT