Hello,

How can I declare an array as a class variable? For example I can do:

<?php
class test {

private $name = 'john';

public function getName() {
  return $this->name;
} 

}
?>

But how can I make the following work?

<?php
class test {

private $name = array();
private $name[] = 'john';
private $name[] = 'jenny';

public function getName() {
  return $this->name;
} 

}
?>

Recommended Answers

All 3 Replies

you need to use a constuctor

<?php
class test {
  var $name = array();
  //this function used to make class PHP4 compatible
  function test() {
    $this->__construct();
  }
  function __construct() {
    $this->name = array('john','jenny');
  }
  function getName() {
     return $this->name;
  }
}
?>

how can i declare an array variable as a class property

how can i declare an array variable as a class property

See previous post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.