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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lkrubner@geocities.com

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

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

    static $controllerForA ll = new McControllerFor All();

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



    function & getController($ callingCode=fal se) {
    // 11-27-04 - we want to get the controllerForAl l variable, and we
    want to make
    // sure that it is always passed by reference. Rather than having
    functions do
    // something like this:
    //
    // global $controllerForA ll;
    //
    // 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 controllerForAl l to be static, therefore the
    second time
    // this function is called it should already exist.

    if (is_object($con trollerForAll)) {
    if ($callingCode) $controllerForA ll->setCallingCode ($callingCode);
    return $controllerForA ll;
    } else {
    $controllerForA ll = & $GLOBALS["controllerForA ll"];
    if (is_object($con trollerForAll)) {
    if ($callingCode) $controllerForA ll->setCallingCode ($callingCode);
    return $controllerForA ll;
    } else {
    if (class_exists(" McControllerFor All")) {
    static $controllerForA ll = new McControllerFor All();
    if (is_object($con trollerForAll)) {
    if ($callingCode) $controllerForA ll->setCallingCode ($callingCode);

    return $controllerForA ll;
    } else {
    echo "Awful sorry, we were looking for a software object called
    'McControllerFo rAll' 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
    'McControllerFo rAll' but we were unable to find it. The software
    really, really needs it to work.";
    }
    }
    }
    }

  • Norman Peelman

    #2
    Re: how to use the keyword 'static' in PHP 4?

    <lkrubner@geoci ties.com> wrote in message
    news:1103190614 .611168.14090@z 14g2000cwz.goog legroups.com...[color=blue]
    > In the code below I'm getting a parse error on this line:
    >
    > static $controllerForA ll = new McControllerFor All();
    >
    > why does this give me a parse error? I'm running PHP 4.
    >
    >
    >
    > function & getController($ callingCode=fal se) {
    > // 11-27-04 - we want to get the controllerForAl l variable, and we
    > want to make
    > // sure that it is always passed by reference. Rather than having
    > functions do
    > // something like this:
    > //
    > // global $controllerForA ll;
    > //
    > // 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 controllerForAl l to be static, therefore the
    > second time
    > // this function is called it should already exist.
    >
    > if (is_object($con trollerForAll)) {
    > if ($callingCode) $controllerForA ll->setCallingCode ($callingCode);
    > return $controllerForA ll;
    > } else {
    > $controllerForA ll = & $GLOBALS["controllerForA ll"];
    > if (is_object($con trollerForAll)) {
    > if ($callingCode) $controllerForA ll->setCallingCode ($callingCode);
    > return $controllerForA ll;
    > } else {
    > if (class_exists(" McControllerFor All")) {
    > static $controllerForA ll = new McControllerFor All();
    > if (is_object($con trollerForAll)) {
    > if ($callingCode) $controllerForA ll->setCallingCode ($callingCode);
    >
    > return $controllerForA ll;
    > } else {
    > echo "Awful sorry, we were looking for a software object called
    > 'McControllerFo rAll' 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
    > 'McControllerFo rAll' but we were unable to find it. The software
    > really, really needs it to work.";
    > }
    > }
    > }
    > }
    >[/color]

    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(" McControllerFor All")) {
    $tmp = new McControllerFor All();
    static $controllerForA ll = $tmp;
    }


    Check out this link...


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



    Comment

    • Janwillem Borleffs

      #3
      Re: how to use the keyword 'static' in PHP 4?

      Norman Peelman wrote:[color=blue]
      > <lkrubner@geoci ties.com> wrote in message
      > news:1103190614 .611168.14090@z 14g2000cwz.goog legroups.com...[color=green]
      >> In the code below I'm getting a parse error on this line:
      >>
      >> static $controllerForA ll = new McControllerFor All();
      >>
      >> why does this give me a parse error? I'm running PHP 4.
      >>[/color][/color]

      You will have to do it in two steps:

      static $controllerForA ll ;
      $controllerForA ll = new McControllerFor All();


      JW



      Comment

      • lkrubner@geocities.com

        #4
        Re: how to use the keyword 'static' in PHP 4?

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

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

        Comment

        Working...