Re: How to declare a static object in PHP?



On May 23, 4:23 pm, ngxfer...@xxxxxxxxx wrote:
Hi experts,

I am new to PHP, I have a HashMap class and want to declare as static
in my other class,

class web
{
public static $map = new HashMap();

}

somehow the compiler is complaining. It only allows me to do the
following:

class web
{
public static $map;

public __construct()
{
map = new HashMap();
}

}

but I don't want this, sorry for my stupid questions, i was coming
from C++ and Java background, so I am not quite familar with the
syntax in PHP.

Thanks alot

Best regards
ferdinand

just two easy changes:



class web
{
public static $map;

public function __construct()
{
self::$map = new HashMap();
}

}

- you have to access static object properties via self:: not via $this-

- and the construct needs the function keyword, like any other
function in php

kind regards
daniel
.



Relevant Pages

  • How to declare a static object in PHP?
    ... I am new to PHP, I have a HashMap class and want to declare as static ... public static $map = new HashMap; ...
    (php.general)
  • Re: [PHP] Re: fwrite/fclose troubles
    ... PHP Warning: fwrite: supplied argument is not a valid stream resource ... public function setLog{ ... I haven't quite gotten my head around some of the stuff I've been reading about, ...
    (php.general)
  • Re: [PHP] Opinions / Votes Needed
    ... need added in to PHP 6, ... public bool $primativeBool; // primative typed property ... do we get a fatal error as we do with typehinted parameters? ... public function setObj{ ...
    (php.general)
  • Re: Object Oriented Content System - the idea
    ... > be shared among subsequent requests is any indication I assumed PHP re-parsed ... global map: It only needs to be checked to be in sinc with the file at ... PHP5 has two ways to do this: you can either use member ... Order with a multi value propety 'orderLines' of type 'OrderLine', ...
    (comp.lang.php)
  • Re: Getting the names of variables passed to functions
    ... I'd like the function foo to return a string of the variable name ... but as far as I know PHP doesn't support them. ... public function getValue() ... public var $bar; ...
    (comp.lang.php)