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

Testing if MyObject is a copy or reference

P: n/a
Hi,

I've started to make use of references for objects and arrays while
putting together a new site framework and am wondering if there is any
sort of debug code that I could put in to my objects to help show if
they are actually copies or references e.g. retrieving memory address
of object or it's internal object id.

For example:

class MyObject {
function getObjectId() { return object_id($this); }
function getMemoryAddress() { return memory_address($this); }
}

Or maybe there is another way at tackling this.

The reason I ask is I'd like to put debug code in to make sure I'm
using references as much as possible and haven't missed &s in places.

I'm not sure if this information is even available in PHP.
Thanks for any advice you can give me,

Steve

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Steve" <st***********@gmail.com> wrote in message
news:10*********************@z14g2000cwz.googlegro ups.com...
Hi,

I've started to make use of references for objects and arrays while
putting together a new site framework and am wondering if there is any
sort of debug code that I could put in to my objects to help show if
they are actually copies or references e.g. retrieving memory address
of object or it's internal object id.

For example:

class MyObject {
function getObjectId() { return object_id($this); }
function getMemoryAddress() { return memory_address($this); }
}

Or maybe there is another way at tackling this.

The reason I ask is I'd like to put debug code in to make sure I'm
using references as much as possible and haven't missed &s in places.

I'm not sure if this information is even available in PHP.
Thanks for any advice you can give me,

Steve


Nope, there isn't a way. Using references when you don't have to is kinda
dumb anyway, as they make your script slower and *more* memory intensive.
Jul 17 '05 #2

P: n/a
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<eY********************@comcast.com>...
"Steve" <st***********@gmail.com> wrote in message
news:10*********************@z14g2000cwz.googlegro ups.com...
Hi,

I've started to make use of references for objects and arrays while
putting together a new site framework and am wondering if there is any
sort of debug code that I could put in to my objects to help show if
they are actually copies or references e.g. retrieving memory address
of object or it's internal object id.


Nope, there isn't a way. Using references when you don't have to is kinda
dumb anyway, as they make your script slower and *more* memory intensive.


Oh well. Will have to stick with good old code reviews then.

I've read a number of articles and they all recommended using
references for objects and large arrays, which is what I am trying to
accomplish as stated in my original post. I know that's it's
considered bad practise to use them for basic variable types such as
ints and strings etc.

Is this not the case - should I only be using them for large objects?
Jul 17 '05 #3

P: n/a
"Steve" <st***********@gmail.com> wrote in message
news:3e**************************@posting.google.c om...
Oh well. Will have to stick with good old code reviews then.

I've read a number of articles and they all recommended using
references for objects and large arrays, which is what I am trying to
accomplish as stated in my original post. I know that's it's
considered bad practise to use them for basic variable types such as
ints and strings etc.

Is this not the case - should I only be using them for large objects?


Well, the people who wrote these articles don't know what they're talking
about. PHP4 uses a copy-on-write strategy for assignment-by-copy. Say you
have the following:

$a = array(...);
$b = $a;

The assignment of $a to $b doesn't actually result in the content of $a
being copied. It's only when $b is modified that a copy occur. Likewise,
when you declare a function argument without using &, the function doesn't
get a new copy of that array, string, or whatever unless the function
modifies the variable. That's one of the reasons why PHP4 is much faster
than PHP3, which always makes physical copies on assignment.

In theory, using reference on large objects has some memory saving because
PHP considers a method invocation a write. The saving is probably
insignificant though, since memory copy is very fast on modern hardware.
Jul 17 '05 #4

P: n/a
Hi Steve,
if there is any sort of debug code that I could put in to my objects
to help show if they are actually copies or references


What about this:

function referencesTheSameObject(&$firstObject, &$secondObject)
{
//the name gbplxzsjj is arbitrary but should be an unlikely name
if(isSet($firstObject->gbplxzsjj) || isSet($secondObject->gbplxzsjj))
trigger_error('copy checking field name collision', E_USER_WARNING);
$firstObject->gbplxzsjj = true;
$result = isSet($secondObject->gbplxzsjj);
unSet($firstObject->gbplxzsjj);
return $result;
}

OK, it is a pritty dirty to hack a field value from the outside into
some arbitrary object, but for unit testing, knowing whether you got a
reference the same object or a copy can just be more important then
being well-behaved ;-)

However, i would rather not put (calls to) this in any other place then
unit tests. There are several unit testing tools available for download,
it should not be hard to add a conditional fail() call and some more
parameter type checks to the above to make it into an object identity
assertion function. Or you can wait for next pnt/unit beta.

Greetings,

Henk Verhoeven,
www.phpPeanuts.org.

Jul 17 '05 #5

P: n/a
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<Y-********************@comcast.com>...

The assignment of $a to $b doesn't actually result in the content of $a
being copied. It's only when $b is modified that a copy occur. Likewise,
when you declare a function argument without using &, the function doesn't
get a new copy of that array, string, or whatever unless the function
modifies the variable. That's one of the reasons why PHP4 is much faster
than PHP3, which always makes physical copies on assignment.

In theory, using reference on large objects has some memory saving because
PHP considers a method invocation a write. The saving is probably
insignificant though, since memory copy is very fast on modern hardware.


That's the first time I've heard that but after finding out what to
search for I did see a number of articles that talk about it. Thanks
for the info Chung, very much appreciated.

Thanks for the example as well Henk - it doesn't look like I will be
needing it for this but it might come in useful for other things.
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.