Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

pws vs iis vs apache, Trap timeout pdksh, nu vot, WWWXXXAPC, wwwxxxapc, WWWXXXAPC site:board.issociate.de, WWWXXXAPC site:board.issociate.de, WWWXXXAPC site:board.issociate.de, WWWXXXAPC site:board.issociate.de, iis is shit

Links

XODOX
Impressum

#1: Static method variable

Posted on 2008-09-18 15:25:35 by Christoph Boget

Perhaps I'm misunderstanding what a static method variable is supposed
to do. I thought the value would be static for an class' instance but
it appears it is static across all instances of the class. Consider:

class StaticTest
{
public function __construct()
{
}

public function test( $newVal )
{
static $retval = '';

if( $retval == '' )
{
$retval = $newVal;
}
echo $retval . '<br>';
}
}

$one = new StaticTest();
$one->test( 'joe' );
$two = new StaticTest();
$two->test( 'bob' );

Should it be working that way?

thnx,
Chris

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Report this message

#2: Re: Static method variable

Posted on 2008-09-18 15:35:10 by Lens Development

Quoting Christoph Boget <christoph.boget@gmail.com>:

> Perhaps I'm misunderstanding what a static method variable is supposed
> to do. I thought the value would be static for an class' instance but
> it appears it is static across all instances of the class. Consider:
>
> class StaticTest
> {
> public function __construct()
> {
> }
>
> public function test( $newVal )
> {
> static $retval = '';
>
> if( $retval == '' )
> {
> $retval = $newVal;
> }
> echo $retval . '<br>';
> }
> }
>
> $one = new StaticTest();
> $one->test( 'joe' );
> $two = new StaticTest();
> $two->test( 'bob' );
>
> Should it be working that way?
>
> thnx,
> Chris
>

That's exactly how a static class var functions. It's available cross
all instances of the class.



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Report this message

#3: Re: Static method variable

Posted on 2008-09-18 15:37:36 by Carlos Medina

Christoph Boget schrieb:
> Perhaps I'm misunderstanding what a static method variable is supposed
> to do. I thought the value would be static for an class' instance but
> it appears it is static across all instances of the class. Consider:
>
> class StaticTest
> {
> public function __construct()
> {
> }
>
> public function test( $newVal )
> {
> static $retval = '';
>
> if( $retval == '' )
> {
> $retval = $newVal;
> }
> echo $retval . '<br>';
> }
> }
>
> $one = new StaticTest();
> $one->test( 'joe' );
> $two = new StaticTest();
> $two->test( 'bob' );
>
> Should it be working that way?
>
> thnx,
> Chris
Hi Chris,
please read here. If you have questions after that. Write again please:

http://de3.php.net/manual/en/language.oop5.static.php

Regards

Carlos

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Report this message

#4: Re: Static method variable

Posted on 2008-09-18 16:06:33 by Micah Gersten

If you're looking for a persistent variable in one class instance, then
you need a member variable. If you want it persistent across all class
instances, you want a static variable.

Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com



Christoph Boget wrote:
> Perhaps I'm misunderstanding what a static method variable is supposed
> to do. I thought the value would be static for an class' instance but
> it appears it is static across all instances of the class. Consider:
>
> class StaticTest
> {
> public function __construct()
> {
> }
>
> public function test( $newVal )
> {
> static $retval = '';
>
> if( $retval == '' )
> {
> $retval = $newVal;
> }
> echo $retval . '<br>';
> }
> }
>
> $one = new StaticTest();
> $one->test( 'joe' );
> $two = new StaticTest();
> $two->test( 'bob' );
>
> Should it be working that way?
>
> thnx,
> Chris
>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Report this message

#5: Re: Static method variable

Posted on 2008-09-18 21:01:49 by Jochem Maas

Christoph Boget schreef:
> Perhaps I'm misunderstanding what a static method variable is supposed
> to do. I thought the value would be static for an class' instance but
> it appears it is static across all instances of the class. Consider:
>
> class StaticTest
> {
> public function __construct()
> {
> }
>
> public function test( $newVal )
> {
> static $retval = '';
>
> if( $retval == '' )
> {
> $retval = $newVal;
> }
> echo $retval . '<br>';
> }
> }
>
> $one = new StaticTest();
> $one->test( 'joe' );
> $two = new StaticTest();
> $two->test( 'bob' );
>
> Should it be working that way?

yes. it's a function variable that retains state. everyone misread your
opst so far ... this has nothing to do with static class scope, although
it does work in the same way.

you can do the exact same thing with a normal function:

function test( $newVal )
{
static $retval;

if(!isset($retval))
$retval = $newVal;

echo $retval . "\n";
}

test('joe');
test('bob');



>
> thnx,
> Chris
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Report this message