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

Recommended Answers

All 2 Replies

//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 );

Great! Thanks for your solution!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.