473,466 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to statically save a object reference in an object????

I would like to use a static variable inside a method in an object to
hold a reference to another object. But i can't get it to work. Here
is an example

class B{
var $test;
}
class A{
function hold($a){
static $ref;
if(is_object($a)){
$ref = $a;
}
print $ref->test;
}
}

$a = new A;
$b = new B;
$b->test = 10;
$a->hold($b);
$b->test = 11;
$a->hold();

output
------
10
10

Should be
---------
10
11

If i try passing it in as a refrence the second call gives nothing as
if it forgot about it.

class A{
function hold(&$a){
static $ref = 0;
if(is_object($a)){
$ref = &$a;
}
print $ref->test;
}
}

output
------
10
0

Can someone help me here

Thanks
Jul 17 '05 #1
2 1906
Ryan Hubbard writes:
I would like to use a static variable inside a method in an object to
hold a reference to another object. But i can't get it to work. Here
is an example class B{
var $test;
}
class A{
function hold($a){
static $ref;
if(is_object($a)){
$ref = $a;
}
print $ref->test;
}
}
This one won't work because you are passing a copy of the object, not
a reference. The same code will work the way you expect in PHP5 where
the value copied by the assignment $ref = $a will be a handle
identifying the object rather than the object itself.
If i try passing it in as a refrence the second call gives nothing as
if it forgot about it. class A{
function hold(&$a){
static $ref = 0;
if(is_object($a)){
$ref = &$a;
}
print $ref->test;
}
}


You might expect this one to work but it doesn't, because references
aren't pointers, and because of the way static variables are
implemented in PHP. "$ref = &$a" does not mean "$ref holds a
reference to $a" rather it means "$ref now references the same address
as $a". The consequence of this is that the symbol $ref no longer
references the static area of memory assigned to it when it was
declared. When you re-enter the function hold() the symbol $ref is
reinitialised to reference the same area of memory that it did in the
first call, which is unchanged.

The only way to preserve an object between calls is to copy it to the
area of memory assigned to $ref in the static declaration, as in your
first example. If hold() returns a reference then you can affect the
static value from outside the function that declared it.

Like this
class B{
var $test;
}

class A{
function &hold($a){
static $ref;
if(is_object($a)){
$ref = $a;
}
print $ref->test;

return $ref;
}
}

$a = new A;
$b = new B;
$b->test = 10;
$b =& $a->hold($b); // $b and $ref now reference the same area of memory.
$b->test = 11;
$a->hold(); // This will work now.

--

__o Alex Farran
_`\<,_ Analyst / Programmer
(_)/ (_) www.alexfarran.com

Jul 17 '05 #2
Hey thanks alot. That one was driving me crazy. PHP is very strange
how it handles references.

Alex Farran <al**@alexfarran.com> wrote in message news:<m3************@alexfarran.com>...
Ryan Hubbard writes:
I would like to use a static variable inside a method in an object to
hold a reference to another object. But i can't get it to work. Here
is an example

class B{
var $test;
}
class A{
function hold($a){
static $ref;
if(is_object($a)){
$ref = $a;
}
print $ref->test;
}
}


This one won't work because you are passing a copy of the object, not
a reference. The same code will work the way you expect in PHP5 where
the value copied by the assignment $ref = $a will be a handle
identifying the object rather than the object itself.
If i try passing it in as a refrence the second call gives nothing as
if it forgot about it.

class A{
function hold(&$a){
static $ref = 0;
if(is_object($a)){
$ref = &$a;
}
print $ref->test;
}
}


You might expect this one to work but it doesn't, because references
aren't pointers, and because of the way static variables are
implemented in PHP. "$ref = &$a" does not mean "$ref holds a
reference to $a" rather it means "$ref now references the same address
as $a". The consequence of this is that the symbol $ref no longer
references the static area of memory assigned to it when it was
declared. When you re-enter the function hold() the symbol $ref is
reinitialised to reference the same area of memory that it did in the
first call, which is unchanged.

The only way to preserve an object between calls is to copy it to the
area of memory assigned to $ref in the static declaration, as in your
first example. If hold() returns a reference then you can affect the
static value from outside the function that declared it.

Like this
class B{
var $test;
}

class A{
function &hold($a){
static $ref;
if(is_object($a)){
$ref = $a;
}
print $ref->test;

return $ref;
}
}

$a = new A;
$b = new B;
$b->test = 10;
$b =& $a->hold($b); // $b and $ref now reference the same area of memory.
$b->test = 11;
$a->hold(); // This will work now.

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: joe martin | last post by:
Does anyone know when or if destructors for statically declared objects are called? I am just wondering if I am leaking system resources each time i exit my program. Thanks, -Joe ...
4
by: RobG | last post by:
I have a function whose parameter is a reference the element that called it: function someFunction(el) { ... } The function is assigned to the onclick event of some elements in the HTML...
4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
2
by: oxblood | last post by:
Hi, I'm trying to statically link a library I creat with 'ar rcs mylib.a .o files....' If I link only the .o files with my .c file, I get no compile error and the program runs fine. But if I...
1
by: Bob Rock | last post by:
Hello, I have a web method that serializes a class that includes among its public fields a System.Object field to which, at runtime, I assign instances of various different classes. When...
7
by: Lee Crabtree | last post by:
I'm starting work on what will eventually be a very, very LARGE project. A lot of the project involves taking C/C++ class libraries and wrapping them with managed C++. I'd like to minimize the...
2
by: fangee | last post by:
Hi everybody, I'm facing a problem trying to statically compile a simple c++ prog, something like: int main(){ return 1; } Using g++ 2.95.4 (I must use this to compile a much more complex...
8
by: Gilles Ganault | last post by:
Hello I need to compile PHP5 with SQLite statically and without PDO. I don't need DB-independence, so it's fine using sqlite_*() functions instead of PDO. 1. I've downloaded, compiled, and...
0
by: AK | last post by:
Hello, I need to do the following with an xml document which has a list of assets: 1. Hash the assets 2. Hash the element describing the assets 3. Create a digital signature (using X.509...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
1
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.