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

PHP Ojbect Reference in a functions static variable

Could someone provide me with some code to hold an object reference in
a static variable in a function.

function a(&$t){
static $b;

if(is_object($b)){
print "IN - $b->logfile";
} else {
print "Setting<BR>";
$b = &$t;
print "IN - $b->logfile<BR>";
}
}

calling a with a object reference then should hold its reference and
if called with no args it should print out the references variable.
But when called with no aguments the static varible b is null. Can
some one help be out here. Thanks

$text->logfile = "TEST";
a(&$test);
a();

output
------
Setting
IN - TEST
Setting
IN - TEST
Jul 17 '05 #1
2 2446
Ryan Hubbard wrote:
Could someone provide me with some code to hold an object reference in
a static variable in a function.

function a(&$t){
static $b;

if(is_object($b)){
print "IN - $b->logfile";
} else {
print "Setting<BR>";
$b = &$t;
print "IN - $b->logfile<BR>";
}
}

calling a with a object reference then should hold its reference and
if called with no args it should print out the references variable.
But when called with no aguments the static varible b is null. Can
some one help be out here. Thanks

$text->logfile = "TEST";
a(&$test);
a();

output
------
Setting
IN - TEST
Setting
IN - TEST


Calling a() without any parameters causes PHP to issue a warning because
you have defined a required parameter for the function "a". When you
declare a variable in a function to be static, it is only static for the
life of the function. Once the function exits, all values associated
with it are marked for deletion. Try the following modification to your
code and you should see the difference.

<?php

static $b;

function a(&$t){
if(is_object($GLOBALS['b'])){
print "IN - " . $GLOBALS['b']->logfile . "\n";
} else {
print "Setting\n";
$GLOBALS['b'] =& $t;
print "IN - " . $GLOBALS['b']->logfile . "\n";
}
}

class test {
var $logfile;
}
$test = new test();
$test->logfile = "TEST";
a($test);
a($test);

?>

output
-----------
Setting
IN - TEST
IN - TEST
--
Amir Khawaja.

----------------------------------
Rules are written for those who lack the ability to truly reason, But
for those who can, the rules become nothing more than guidelines, And
live their lives governed not by rules but by reason.
- James McGuigan
Jul 17 '05 #2
Static variables are not deleted upon exiting the function. The value
will stay and only the initial static declaration inside the function
are run the first time through the function. Example

function test(){
static a = 0;
a++;
return a;
}

print test();
print test();
print test();

output
------
1
2
3

Yes it will work with a global variable but I wish not to use globals
for a very good reason. The problem here are static variables
combined with object references. Can anyone help.

Thanks
Amir Khawaja <am**@gorebels.net> wrote in message news:<EKPUb.25299$QJ3.875@fed1read04>...
Ryan Hubbard wrote:
Could someone provide me with some code to hold an object reference in
a static variable in a function.

function a(&$t){
static $b;

if(is_object($b)){
print "IN - $b->logfile";
} else {
print "Setting<BR>";
$b = &$t;
print "IN - $b->logfile<BR>";
}
}

calling a with a object reference then should hold its reference and
if called with no args it should print out the references variable.
But when called with no aguments the static varible b is null. Can
some one help be out here. Thanks

$text->logfile = "TEST";
a(&$test);
a();

output
------
Setting
IN - TEST
Setting
IN - TEST


Calling a() without any parameters causes PHP to issue a warning because
you have defined a required parameter for the function "a". When you
declare a variable in a function to be static, it is only static for the
life of the function. Once the function exits, all values associated
with it are marked for deletion. Try the following modification to your
code and you should see the difference.

<?php

static $b;

function a(&$t){
if(is_object($GLOBALS['b'])){
print "IN - " . $GLOBALS['b']->logfile . "\n";
} else {
print "Setting\n";
$GLOBALS['b'] =& $t;
print "IN - " . $GLOBALS['b']->logfile . "\n";
}
}

class test {
var $logfile;
}
$test = new test();
$test->logfile = "TEST";
a($test);
a($test);

?>

output
-----------
Setting
IN - TEST
IN - TEST

Jul 17 '05 #3

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

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
8
by: Sreenivas | last post by:
Hi, We cannot return a reference to an automatic variable from a function, as per the ANSI C++ standard the behaviour is undefined. Does this hold for inline functions too? or can I return a...
26
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function...
5
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
10
by: PCHOME | last post by:
Hi! Would someone please help me thess C error(in gcc on Linux)? The compiler continues to give me: readLP.o: In function `Input_Problem': readLP.o(.text+0x0): multiple definition of...
5
by: amparikh | last post by:
I have some test code which demonstrates the problem. I know I could solve this by just returning a pointer, but I better use a reference. In real code, what I actually want to return is a...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: Cirene | last post by:
I just installed VS.NET 2008 w/SP1 and the MySQL Connector 5.2. Does the DataSet feature work with MySQL? I created a new dataset, DataSet1.xsd and tried to add a TableAdapter. In the TableAdapter...
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.