0

Hi! Is it possible to pass objects like you do in JavaScript in PHP:

function example($args){
   ...
}

example({foo: 'bar', lorem: 'ipsum'});

Or something similar to this? So that you both get an "infinite" number of arguments and a chosen name for the variable ($args). I know you can use an array but I prefer the JavaScript syntax of objects because it's clearer and less to type :)

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by gunnarflax
1
//you can use an associative array and retrieve $args['foo'] $args['lorem'] within the function
$a=array("foo"=>'bar', "lorem"=>'ipsum');
example( $a );

//OR you can cast it to a "generic" object and access it at $args->foo and $args->lorem 
$a= (object) array("foo"=>'bar', "lorem"=>'ipsum');
example( $a );
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.