[wp-hackers] Re: 2.0.10 and 2.1.3 Release Candidates

Jamie Talbot wphackers at jamietalbot.com
Sun Mar 18 01:29:35 GMT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sabin Iacob wrote:
> Robin Adrianse wrote:
>> Does PHP 4 have support for the static and self keywords?
>>
>> On 3/17/07, Sabin Iacob <iacobs at m0n5t3r.info> wrote:
> 
> static yes, self no; and static is supported only in function scope, not
> in class scope; so, to store something statically in a class (instead of
> making it global, for instance), you would need to to something like this:
> 
> class aaa {
>    function &bbb(){
>        static $_kk;
>        if(!isset($_kk)){
>            $_kk = "";
>        }
>        return $_kk;
>    }
> }
> 
> $kk = &aaa::bbb();
> echo var_export($kk, true), "\n";
> 
> $kk .= "asdfasfasfdasfd";
> 
> echo var_export(aaa::bbb(), true), "\n";
> 
> which outputs
> 
> ''
> 'asdfasfasfdasfd'

Actually, you can simulate static class variables so that they are directly accessible, IIRC.

class myclass {
  var $my_static_var;

  function myclass() {
   static $my_static_var;

   $this->my_static_var = & $my_static_var;
  }
}

Then:

$instance = new myclass();
echo $instance->my_static_var;

should work as expected, remaining static for each instance.  I prefer this method as it avoids the
overhead of a function call.  This still works for PHP5 too, though it's not required as you say.

(Disclaimer: written off the top of my head without a debugger in sight - some tweaking might be
necessary)

The last I heard, the static keyword is also going to be reused for very late static binding in
PHP6, where it will reference the calling class, rather than the class the code is executed inside.
 I don't know what the current status of this decision is, but it might be something to bear in mind
when using static/self for function calls in current projects...[1]

Cheers,

Jamie.

[1]
http://www.php.net/~derick/meeting-notes.html#late-static-binding-using-this-without-or-perhaps-with-a-different-name

- --
http://jamietalbot.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF/JX+rovxfShShFARAk/LAJsEgQPINWUS37goYJvDN963XgfYUQCfboEt
kiKaBHoRqk+euEqcn5NkLLE=
=9ToQ
-----END PGP SIGNATURE-----


More information about the wp-hackers mailing list