List Info

Thread: Static members or not?




Static members or not?
user name
2006-10-26 09:49:38
Hi
I am using PHP5 and the static members/variables doesn't
seem to stick.
PHP5 doesn't seem to care about the static keyword. It would
be nice to
get this working.

For example: In the code below, I never get a return before
the
print-line.

function getSelectedColumns($prj_id, $page)
{
        static $returns;

        // poor man's caching system
        if (!empty($returns[$prj_id][$page]))
            return $returns[$prj_id][$page];

	print("I always get here!");
        ...
       
$returns[$prj_id][$page][$field_name]="Jello"; 
        ...
}


+-------------------------------------------------------+
  Jostein Martinsen
  System developer, Redpill AB
  Tel: 0734-241 475, 054-4005 142 
  E-mail: jostein.martinsenredpill.se
 

-- 
Eventum Users Mailing List
For list archives: http://lists.mys
ql.com/eventum-users
To unsubscribe:    http:
//lists.mysql.com/eventum-users?unsub=bondyahoo.com

Static members or not?
user name
2006-10-26 15:35:22
Hi Jostein, is this a problem with the specific code below
or all cases
where static is used?

In my test on PHP 5, static variables work as expected.

Class monkey {
    function testStatic($foo)
    {
        static $blah;

        if (isset($blah[$foo])) {
            return "Static variable used<br
/>";
        }
        $blah[$foo] = 'FOOO';
        return 'No static<br />';

    }
}

echo monkey::testStatic(1);
echo monkey::testStatic(1);

echo "Version: " . PHP_VERSION;

Outputs:
No static
Static variable used
Version: 5.1.2

/bryan

Jostein Martinsen wrote:
> Hi
> I am using PHP5 and the static members/variables
doesn't seem to stick.
> PHP5 doesn't seem to care about the static keyword. It
would be nice to
> get this working.
> 
> For example: In the code below, I never get a return
before the
> print-line.
> 
> function getSelectedColumns($prj_id, $page)
> {
>         static $returns;
> 
>         // poor man's caching system
>         if (!empty($returns[$prj_id][$page]))
>             return $returns[$prj_id][$page];
> 
> 	print("I always get here!");
>         ...
>        
$returns[$prj_id][$page][$field_name]="Jello"; 
>         ...
> }
> 
> 
>
+-------------------------------------------------------+
>   Jostein Martinsen
>   System developer, Redpill AB
>   Tel: 0734-241 475, 054-4005 142 
>   E-mail: jostein.martinsenredpill.se
>  
> 

-- 
Bryan Alsdorf, Manager of Support Systems
MySQL AB, www.mysql.com

Are you MySQL certified?  www.mysql.com/certification

-- 
Eventum Users Mailing List
For list archives: http://lists.mys
ql.com/eventum-users
To unsubscribe:    http:
//lists.mysql.com/eventum-users?unsub=bondyahoo.com

Static members or not?
user name
2006-10-26 15:52:32
Ok, so the static members die on page reload.
Then I must say it works fine. 
It's indeed a 'poor man's caching system' :(


On Thu, 26 Oct 2006 10:35:22 -0500
Bryan Alsdorf <bryanmysql.com> wrote:

> Hi Jostein, is this a problem with the specific code
below or all
> cases where static is used?
> 
> In my test on PHP 5, static variables work as expected.
> 
> Class monkey {
>     function testStatic($foo)
>     {
>         static $blah;
> 
>         if (isset($blah[$foo])) {
>             return "Static variable used<br
/>";
>         }
>         $blah[$foo] = 'FOOO';
>         return 'No static<br />';
> 
>     }
> }
> 
> echo monkey::testStatic(1);
> echo monkey::testStatic(1);
> 
> echo "Version: " . PHP_VERSION;
> 
> Outputs:
> No static
> Static variable used
> Version: 5.1.2
> 
> /bryan
> 
> Jostein Martinsen wrote:
> > Hi
> > I am using PHP5 and the static members/variables
doesn't seem to
> > stick. PHP5 doesn't seem to care about the static
keyword. It would
> > be nice to get this working.
> > 
> > For example: In the code below, I never get a
return before the
> > print-line.
> > 
> > function getSelectedColumns($prj_id, $page)
> > {
> >         static $returns;
> > 
> >         // poor man's caching system
> >         if (!empty($returns[$prj_id][$page]))
> >             return $returns[$prj_id][$page];
> > 
> > 	print("I always get here!");
> >         ...
> >        
$returns[$prj_id][$page][$field_name]="Jello"; 
> >         ...
> > }
> > 
> > 
> >
+-------------------------------------------------------+
> >   Jostein Martinsen
> >   System developer, Redpill AB
> >   Tel: 0734-241 475, 054-4005 142 
> >   E-mail: jostein.martinsenredpill.se
> >  
> > 
> 





+-------------------------------------------------------+
  Jostein Martinsen
  System developer, Redpill AB
  Tel: 0734-241 475, 054-4005 142 
  E-mail: jostein.martinsenredpill.se
 

-- 
Eventum Users Mailing List
For list archives: http://lists.mys
ql.com/eventum-users
To unsubscribe:    http:
//lists.mysql.com/eventum-users?unsub=bondyahoo.com

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )