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

how to use the keyword 'static' in PHP 4?

P: n/a
In the code below I'm getting a parse error on this line:

static $controllerForAll = new McControllerForAll();

why does this give me a parse error? I'm running PHP 4.

function & getController($callingCode=false) {
// 11-27-04 - we want to get the controllerForAll variable, and we
want to make
// sure that it is always passed by reference. Rather than having
functions do
// something like this:
//
// global $controllerForAll;
//
// we instead want them to use this function. This adds a wrapper to
the way we
// this variable and thus allows more flexibility in changing the
access later.
// After all, it may not remain in global space in future versions of
the
// software.

// 12-12-04 - we declare controllerForAll to be static, therefore the
second time
// this function is called it should already exist.

if (is_object($controllerForAll)) {
if ($callingCode) $controllerForAll->setCallingCode($callingCode);
return $controllerForAll;
} else {
$controllerForAll = & $GLOBALS["controllerForAll"];
if (is_object($controllerForAll)) {
if ($callingCode) $controllerForAll->setCallingCode($callingCode);
return $controllerForAll;
} else {
if (class_exists("McControllerForAll")) {
static $controllerForAll = new McControllerForAll();
if (is_object($controllerForAll)) {
if ($callingCode) $controllerForAll->setCallingCode($callingCode);

return $controllerForAll;
} else {
echo "Awful sorry, we were looking for a software object called
'McControllerForAll' but we were unable to find it. The software
really, really needs it to work.";
}
} else {
echo "Awful sorry, we were looking for a software object called
'McControllerForAll' but we were unable to find it. The software
really, really needs it to work.";
}
}
}
}

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


P: n/a
<lk******@geocities.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
In the code below I'm getting a parse error on this line:

static $controllerForAll = new McControllerForAll();

why does this give me a parse error? I'm running PHP 4.

function & getController($callingCode=false) {
// 11-27-04 - we want to get the controllerForAll variable, and we
want to make
// sure that it is always passed by reference. Rather than having
functions do
// something like this:
//
// global $controllerForAll;
//
// we instead want them to use this function. This adds a wrapper to
the way we
// this variable and thus allows more flexibility in changing the
access later.
// After all, it may not remain in global space in future versions of
the
// software.

// 12-12-04 - we declare controllerForAll to be static, therefore the
second time
// this function is called it should already exist.

if (is_object($controllerForAll)) {
if ($callingCode) $controllerForAll->setCallingCode($callingCode);
return $controllerForAll;
} else {
$controllerForAll = & $GLOBALS["controllerForAll"];
if (is_object($controllerForAll)) {
if ($callingCode) $controllerForAll->setCallingCode($callingCode);
return $controllerForAll;
} else {
if (class_exists("McControllerForAll")) {
static $controllerForAll = new McControllerForAll();
if (is_object($controllerForAll)) {
if ($callingCode) $controllerForAll->setCallingCode($callingCode);

return $controllerForAll;
} else {
echo "Awful sorry, we were looking for a software object called
'McControllerForAll' but we were unable to find it. The software
really, really needs it to work.";
}
} else {
echo "Awful sorry, we were looking for a software object called
'McControllerForAll' but we were unable to find it. The software
really, really needs it to work.";
}
}
}
}


I believe that 'static' can only be used for non-computed values... so:

static $var = 1; // is ok
static $var = 1+1; // not ok

try this:

if (class_exists("McControllerForAll")) {
$tmp = new McControllerForAll();
static $controllerForAll = $tmp;
}
Check out this link...
http://us2.php.net/manual/en/languag...bles.scope.php

Norm
---
Avatar Hosting at www.easyavatar.com

Jul 17 '05 #2

P: n/a
Norman Peelman wrote:
<lk******@geocities.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
In the code below I'm getting a parse error on this line:

static $controllerForAll = new McControllerForAll();

why does this give me a parse error? I'm running PHP 4.


You will have to do it in two steps:

static $controllerForAll ;
$controllerForAll = new McControllerForAll();
JW

Jul 17 '05 #3

P: n/a
Thanks. I seemed to have solved the problem by moving this line to the
top of the function:

static $controllerForAll;
I guess it only runs the first time the function is called.

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.