Weird array question

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
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Weird array question

Post by alex.barylski »

This might sound like I'm on drugs, but with the exception of contact C and some tylenol (I'm battling YACD - Yet Another Cold Damnit) I've had no such thing :)

Anyways :)

I have a series of objects stored in an array:

Code: Select all

$my_array[0] = new MyObject1();
$my_array[1] = new MyObject2();
$my_array[2] = new MyObject1();
$my_array[3] = new MyObject3();
The objects are NOT and CANNOT be assigned the index of their position in the array...

Using the following I have called one of the objects member functions:

Code: Select all

$ref =& $my_array[1];
$ref->doSomething();
When doSomething() is called, can you think of anyway, using PHP's RTTI or dynamic code execution abilities to somehow determine the index for $my_array for that given object?

For instance the above example, MyObject2 at index: 1 would need to somehow determine, using doSomething() that it was located at index: 1

Remember: I cannot pass any of these objects the index or anything that would indicate the position at which they reside...

Ideas??? :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

array_search() with the strict flag set?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Doh :oops:

I guess I forgot to mention...that doSomething() function has NO idea it's stored in an array or a scalar...I don't have access to the original array

So I don't think any array_* functions will work...if anything works it'll be a real system hack...likely not possible with PHP...but thats why I figured I'd throw it out there... :)

What I was thinking...is if I could somehow get the $this pointer offset and again somehow determine the offset of it's containing array, I could calculate the index by calculating the offset difference and dividing by 4, assuming each element in an array is a 32 bit pointer internally...

But how to do this in PHP...I mean...I'm pretty sure I'd need the flexability of an interpreted language's RTTI but at the same time...the power of direct memory access...via pointers or system functions...

And as far as I can tell the only likely direct memory access PHP has is working with semaphores... :(

I'm not letting on to all details, there is another approach, but it's ad hoc...or at least I'd rather have some cool memory management functions work some magic...

Ideas?

Cheers :)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I'm really not quite sure what your talking about, but maybe something like this would work.

Code: Select all

$Something[0] = new MyObject1();
$Something[1] = "ID KEY";
$Something[2] = "Extra Data";

$my_array[0] = $Something;

// the same for all the others...
$my_array[1] = new MyObject2();
$my_array[2] = new MyObject1();
$my_array[3] = new MyObject3();
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

agtlewis wrote:I'm really not quite sure what your talking about, but maybe something like this would work.

Code: Select all

$Something[0] = new MyObject1();
$Something[1] = "ID KEY";
$Something[2] = "Extra Data";

$my_array[0] = $Something;

// the same for all the others...
$my_array[1] = new MyObject2();
$my_array[2] = new MyObject1();
$my_array[3] = new MyObject3();
Nope...I'm afraid not 8)

It's all good though...I used a different technique and everything works now :)

Cheers
Post Reply