Bluehost.com Web Hosting $6.95

How to declare a static object in PHP?

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-23-2008
ngxfer001@gmail.com
 
Posts: n/a
Default How to declare a static object in PHP?

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
Reply With Quote
  #2 (permalink)  
Old 05-30-2008
danielMitD@googlemail.com
 
Posts: n/a
Default Re: How to declare a static object in PHP?

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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 05:27 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0