Aaron
Aaron

Reputation: 11693

Is it possible to have an array of elements as a property of a PHP class?

As this does not seam to work?

class Site {
private $PROPERTIES = array(
           "NAME",'STACKOVERFLOW',
                   "URL",'http:/stackoverflow.com'
                  );


    function __construct() {
            $this->props = $PROPERTIES;
    }
    function dumpData() {
            var_dump($this->props)
    }
}

Upvotes: 1

Views: 57

Answers (5)

Jakub
Jakub

Reputation: 20475

I think what you want it a associative array?

private $PROPERTIES = array(
           "NAME" => 'STACKOVERFLOW',
           "URL" => 'http:/stackoverflow.com'
                  );

At least that is how I understood it. Because that would work.

$this->PROPERTIES['NAME'];

** fixed, thanks guys :P Too early for me...

Upvotes: 1

Aurimas Ličkus
Aurimas Ličkus

Reputation: 10074

function __construct() {
        $this->props = $this->PROPERTIES;
}

Upvotes: 0

Nonym
Nonym

Reputation: 6299

In classes, reference class-level variables from functions using $this :

function __construct() {
        $this->props = $this->PROPERTIES;
}

Upvotes: 0

soju
soju

Reputation: 25322

You don't need $props since you already have $PROPERTIES...

Upvotes: 0

KoolKabin
KoolKabin

Reputation: 17703

you can but you missed the way of referencing properties...

instead use:

$this->props = $this->PROPERTIES;

Upvotes: 3

Related Questions