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

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

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

Similar topics

14
by: lawrence | last post by:
To call I would do something like: $headline = McSelectJustOneField::callDatastore("cbHeadline"); Is this the correct use of the static keyword, to implement a Singleton design?
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
5
by: siliconwafer | last post by:
Hi all, I wanted to know that is use of extern keyword mandatory in case of global variables and functions used in other source files? i.e consider a following piece of code from MSDN explaining...
16
by: vaughn | last post by:
What is the 'this' keyeword for? If I'm filling a textbox, what's the difference between this.textbox1.Text = "my text"; and textbox1.Text = "my text"; ? I normally use it w/o the 'this'. Thanks.
14
by: Zeng | last post by:
Would somebody know when we should seal a class? Shouldn't all classes be open up for inheritance? Thanks!
3
by: Danny Din | last post by:
Hi, I have the following code – public class Class1 { public Class1()
54
by: Sahil Malik [MVP] | last post by:
What the heck - I can't find it. A bit shocked to see it missing though. So "Does VB.NET have the yield keyword, or any equivalent of it" ? -- - Sahil Malik Upcoming ADO.NET 2.0 book -...
32
by: lcdgoncalves | last post by:
Hi everyone Is there a real need to use keyword static with functions, if we simply don't declare their prototypes in .h file? Many textbooks avoid to discuss this matter and/or discuss only...
1
by: wizwx | last post by:
I just noticed an interesting implementation of Singleton from Bruce Eckel's "Thinking in C++" vol. 2, pp 620: class Singleton { static Singleton s; public: static Singleton& instance() {...
2
by: DaTurk | last post by:
Hi, I have an interesting issue, well, it's not really an issue, but I'd like to understand the mechanics of what's going on. I have a file, in CLI, which has a class declared, and a static...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.