简体   繁体   中英

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)
    }
}

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

instead use:

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

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

因为你已经拥有$ PROPERTIES,所以你不需要$道具......

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM