tags:

views:

27

answers:

4

I don't want to give an initial value. I want to set these later using the set method

class Duck {
    var int id;
    var set = Array();
}

Any idea how to declare without getting error?

A: 
class Duck { var $int, $set = array(); }
Ollie Saunders
+1  A: 

Your question is marked PHP but it doesn't look like any PHP I know. This is correct PHP:

class Duck {
  private $id;
  private $set = array();
}

You don't need to specify an initial value for $set as in this example but that just means it gets the standard default value of 0, false, array(), etc depending on how it's used so I'm not sure what you're trying to achieve.

You're typically better off being explicit.

cletus
A: 

Much more PHP is:

 class Duck {
      public $id;
 }

 $d = new Duck;
 $d->id = 5;
aviv
A: 

A method could be Typ checking;

private $props = array();
static $types = array ("id" => "is_integer","name" => "is_string");

public function __set($name,$value) 
{
   if(array_key_exists($name, self::$types)) 
   {
      if(call_user_func(self::$types[$name],$value)) 
      {
        $this->props[$name] = $value;
      }
      else 
      {
        print "Type assignment error\n";
        debug_print_backtrace();
      }
   }
}
streetparade