How can I declare an array as class variable?

Reply

Join Date: Jul 2006
Posts: 136
Reputation: ryy705 is an unknown quantity at this point 
Solved Threads: 0
ryy705 ryy705 is offline Offline
Junior Poster

How can I declare an array as class variable?

 
0
  #1
Jul 16th, 2008
Hello,

How can I declare an array as a class variable? For example I can do:
  1. <?php
  2. class test {
  3. private $name = 'john';
  4.  
  5. public function getName() {
  6. return $this->name;
  7. }
  8.  
  9. }
  10. ?>

But how can I make the following work?
  1. <?php
  2. class test {
  3. private $name = array();
  4. private $name[] = 'john';
  5. private $name[] = 'jenny';
  6.  
  7. public function getName() {
  8. return $this->name;
  9. }
  10.  
  11. }
  12. ?>
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: How can I declare an array as class variable?

 
0
  #2
Jul 16th, 2008
you need to use a constuctor

  1. <?php
  2. class test {
  3. var $name = array();
  4. //this function used to make class PHP4 compatible
  5. function test() {
  6. $this->__construct();
  7. }
  8. function __construct() {
  9. $this->name = array('john','jenny');
  10. }
  11. function getName() {
  12. return $this->name;
  13. }
  14. }
  15. ?>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC