JQuery style Constructors in PHP?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

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

Not what he wants, though. He wants to use named arguments that can be specified in arbitrary order – Pekka Jan 13 at 14:52 Yea I was in the middle of updating :/ – RobertPitt Jan 13 at 14:56 The update has a bunch of good information. +1.(Although I would personally provide defaults to the parameters rather than leaving them null)... – ircmaxell Jan 13 at 14:57 This is great, thanks very much!

– McB Jan 13 at 15:02 Updated now, thanks – RobertPitt Jan 13 at 15:04.

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

1... – ircmaxell Jan 13 at 14:54 +1 Beat me to it. – Sohnee Jan 13 at 14:54 wups, thank's for the correction, edited – andreas Jan 13 at 15:00.

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

Using json is nonsensical in this instance. (A native array wouldn't need to be needlessly en/decoded. ) – middaparka Jan 13 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 at 9:21.

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

JQuery is a fast, small, and feature-rich JavaScript library. A multitude of browsers.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions