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

Testing if MyObject is a copy or reference

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
5 1746

"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
"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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
16
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an...
0
by: Tony Johansson | last post by:
Hello! I have two classes called Handle which is a template class and a class Integer which is not a template class. The Integer class is just a wrapper class for a primitive int with some...
2
by: Lawrence San | last post by:
I'm trying to test some simple JavaScript meant to speed up the display of my Web pages for readers using modems, but I have a fast DSL connection and I'm having trouble visualizing how effective...
0
by: Brian Russell | last post by:
We have three servers (beyond my development box) in our organization. The first is a testing server that has IIS and SQL Server on it. The second is another testing server that also has IIS and...
5
by: Jimp | last post by:
Why can't I cast List<MyObject> to ICollection<IMyObject>. MyObject implements IMyObject, and of course, List implements ICollection. Thanks
7
by: Robin Imrie | last post by:
Hi, I have some code like this.... MyObject ^o = gcnew MyObject(); My2ndObject ^obj2 = o->GetObject(); if( obj2 != NULL ) {
30
by: Alf P. Steinbach | last post by:
I once suggested in that SomeOne Else(TM) should propose a string value class that accepted literals and char pointers and so on, with possible custom deleter, and in case of literal strings just...
10
by: lpinho | last post by:
Hi all, I have a class (named for the example myObject) that can be of several types (int, string, float, etc), instead of using a object to define it's type I used a generic. public class...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.