1

Is there a way to instantiate a new PHP object in a similar manner to those in jQuery? I'm talking about assigning a variable number of arguments when creating the object. For example, I know I could do something like:

...
//in my Class
__contruct($name, $height, $eye_colour, $car, $password) {
...
}

$p1 = new person("bob", "5'9", "Blue", "toyota", "password");

But I'd like to set only some of them maybe. So something like:

$p1 = new person({
    name: "bob",
    eyes: "blue"});

Which is more along the lines of how it is done in jQuery and other frameworks. Is this built in to PHP? Is there a way to do it? Or a reason I should avoid it?

McB
  • 1,082
  • 1
  • 18
  • 36
  • possible duplicate of [Ruby-like array arguments implementation in PHP](http://stackoverflow.com/questions/870501/ruby-like-array-arguments-implementation-in-php) – Pekka Jan 13 '11 at 14:50
  • Here is a workaround (including a description of its downsides): http://stackoverflow.com/questions/2112913 – Pekka Jan 13 '11 at 14:51

4 Answers4

4

the best method to do this is using an array:

class Sample
{
    private $first  = "default";
    private $second = "default";
    private $third  = "default";

    function __construct($params = array())
    {
         foreach($params as $key => $value)
         {
              if(isset($this->$key))
              {
                  $this->$key = $value; //Update
              }
         }
    }
}

And then construct with an array

$data = array(
     'first' => "hello"
     //Etc
);
$Object = new Sample($data);
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
2
class foo {
   function __construct($args) {
       foreach($args as $k => $v) $this->$k = $v;
       echo $this->name;
    }
 }

 new foo(array(
    'name' => 'John'
 ));

The closest I could think of.

If you want to be more fancy and just want to allow certain keys, you can use __set() (only on php 5)

var $allowedKeys = array('name', 'age', 'hobby');
public function __set($k, $v) {
   if(in_array($k, $this->allowedKeys)) {
      $this->$k = $v;
   }
}
Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
  • 3
    Variable Variables? Inside of a class? Really? Why not use `$this->$k` instead of `$$k`? Decent idea, wrong implementation... If you were going to do that, why not just use [`extract`](http://us2.php.net/extract)? -1... – ircmaxell Jan 13 '11 at 14:54
0

get args won't work as PHP will see only one argument being passed.

public __contruct($options) {
    $options = json_decode( $options );
    ....
    // list of properties with ternary operator to set default values if not in $options
    ....
}

have a looksee at json_decode()

Ian Wood
  • 6,515
  • 5
  • 34
  • 73
  • Using json is nonsensical in this instance. (A native array wouldn't need to be needlessly en/decoded.) – John Parker Jan 13 '11 at 14:55
  • agreed - but just addressing the OPs initial request. No need to encode json to pass in object instantiation but would always need decode. – Ian Wood Jan 14 '11 at 09:21
-1

The closest I can think of is to use array() and extract().

...
//in your Class
__contruct($options = array()) {

    // default values
    $password = 'password';
    $name = 'Untitled 1';
    $eyes = '#353433';

    // extract the options
    extract ($options);

    // stuff
    ...

}

And when creating it.

$p1 = new person(array(
    'name' => "bob",
    'eyes' => "blue"
));
Thai
  • 10,746
  • 2
  • 45
  • 57