Is it wrong to use an instance of an object as an argument inside a method / function call?

Is it wrong to use an instance of an object as an argument? For example, is this a bad practice for this?

myFunction(new Foo);

Or you should always do it like this:

$foo = new Foo;
myFunction($foo);

My specific scenario.

I ask because I am in a situation where I need to pass a lot of new objects to a method in order to “register” a bunch of plugins. This is something like this:

$imagePlugin = new ImagePlugin;
$videoPlugin = new VideoPlugin;
$audioPlugin = new AudioPlugin;

$myLibrary->registerPlugins(array(
        $imagePlugin,
        $videoPlugin,
        $audioPlugin
    ));

I am wondering if, in order, in accordance with best practices, shorten it to the code below:

$myLibrary->registerPlugins(array(
        new ImagePlugin,
        new VideoPlugin,
        new AudioPlugin
    ));
+4
source share
1 answer

If you do not need them outside the function call, I would say that you are doing it right by creating them directly as arguments.

+2

All Articles