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

Dani AI

Generated

Short answer: PHP has no JavaScript-style inline object literal syntax, but the effect can be achieved several ways. 's reply (using a keyed container) is the classic approach; below are cleaner or more explicit options that work better in modern PHP and a few version notes so the right technique can be chosen for the environment.

A compact, modern way to accept arbitrary named values is to use a variadic parameter and (in PHP 8+) named arguments. A function declared with ...$args will collect any extra positional arguments into an array — and when callers use name: value syntax the names become array keys. Example:

function example(...$args) {
    var_dump($args); // positional values and any named args (string keys)
}

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

This behavior comes from PHP's named-arguments and variadic support. See the manual and RFC for details. PHP named arguments / variadics. (php.net)

If the goal is literal JavaScript-like syntax in source, another quick trick is to write a JSON object literal and decode it to an object at runtime:

$opts = json_decode('{"foo":"bar","lorem":"ipsum"}'); // stdClass by default
echo $opts->foo;

json_decode() returns an object (stdClass) by default or an associative array when asked; that can feel very close to JS object literals. [json_decode manual]. (php.net)

Compatibility and best practice notes: the ... parameter syntax arrived in PHP 5.6, array spread and related unpacking improvements landed later (array spread in PHP 7.4, string-key unpacking improved in PHP 8.1), and named arguments in PHP 8.0 — pick a pattern that matches the minimum PHP version in use. For public APIs prefer explicit, typed parameters or a small DTO class instead of accepting arbitrary bags; that keeps call sites clear and makes maintenance safer. [PHP 5.6 new features], [array spread / unpacking]. (php.net)

References: original question and 's suggestion on keyed containers are a good starting point; use the above patterns where they fit the PHP version and API design goals.

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.