This is a discussion on How to declare a static object in PHP? within the PHP General forums, part of the PHP Programming Forums category; Hi experts, I am new to PHP, I have a HashMap class and want to declare as static in my ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
On May 23, 4:23 pm, ngxfer...@gmail.com 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 |