Jump to content

Recommended Posts

Its been a long while since I've posted a question here, but really, I'm at my wits end.

 

The error in question is: array_merge(): Argument #2 is not an array. I know what the errors means, and I'm sure I have an array.

 

This:

echo '<pre>';
var_dump($this->getOptionObject()->getOption('response', array()));
echo '</pre>';

 

Produces:

[pre]

array(0) {

}

[/pre]

 

The getOption method will return the value of the option passed in as the first argument, or the value of the second argument if not found.

 

This piece of code however:

$this->_preUserEvents = array(
    'Response' => array(
        'namespace' => '\Pm\Bootstrap\Resource',
        'options' =>
            array_merge(
                array(
                    'observers' => $this->getOptionObject()->getOption(
                        'defaultEventListeners',
                        array()
                    )
                ),
                $this->getOptionObject()->getOption(
                    'response',
                    array()
                )
            )
        ),
    // more irrelevant code

 

Produces the error above.

 

Now, the error also only actually shows up when I run my unit tests (via cli):

[pre]

PHPUnit 3.4.5 by Sebastian Bergmann.

 

......................E..

 

Time: 0 seconds, Memory: 9.25Mb

 

There was 1 error:

 

1) Pm\Test\Application\ApplicationTest::testEvents

array_merge(): Argument #2 is not an array

 

/home/trq/devbox/libs/pm/library/Pm/Application.php:558

 

FAILURES!

Tests: 64, Assertions: 113, Errors: 1.

[/pre]

 

It does not show up when I actually bootstrap the framework via a http request, even though the server and cli both have E_ALL error reporting and display errors on.

 

One more thing. If I explicitly typecast the second argument to an array, the errors goes away.

 

array_merge(
    array(
        'observers' => $this->getOptionObject()->getOption(
            'defaultEventListeners',
            array()
        )
    ),
    (array) $this->getOptionObject()->getOption(
        'response',
        array()
    )
)

 

This is of course an unacceptable solution. Anyone got any ideas? It's really got me beat.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/198212-array_merge-error-has-me-baffled/
Share on other sites

Are you missing a close bracket? I've numbed an open bracket, as a number, with the close bracket the same number. Comes out with 1 close bracket is missing..

 

$this->_preUserEvents = array( // This doesn't have a close bracket
    'Response' => array( //5
        'namespace' => '\Pm\Bootstrap\Resource',
        'options' =>
            array_merge(//4
                array( //3
                    'observers' => $this->getOptionObject()->getOption(//2
                        'defaultEventListeners',
                        array()
                    )//2
                ),//3
                $this->getOptionObject()->getOption( //1
                    'response',
                    array()
                ) //1
            ) //4
        ), //5

 

 

I'm not too sure if it is possible. You could put the arrays in a variable?

$array = array();

Which allows you to do

array_merge($array1, $array2)

 

.. I do kind of understand the code.

It confuses me, it looks as if its got an an 'merge array', inside an 'array', inside an 'array'.

Might this be confusing it?

That would result in quite another error message. I would assume thorpe just missed one ) when copying code here..

 

Anyway, perhaps try var_dumping this statement in the context of this test?

 

AFAIR in PHPUnit tests $this refers to instance of test class, and tested object is usually defined as one of variables of $this.

AFAIR in PHPUnit tests $this refers to instance of test class, and tested object is usually defined as one of variables of $this.

 

This code isn't part of the actual test, but is part of the code being tested. We'll actually, the test is written to test something else different entirely, but inadvertently executes this piece of code.

 

The test method is:

public function testEvents()
{
    $pm = new \Pm\Application;
    $pm->init(
        array(
            'context' => 'test',
            'events' => array(
                'Foo' => array(
                    'class' => '\Foo\Boo',
                    'options' => array(1,2,3)
                )
            )
        )
    );

    $this->assertArrayHasKey('Request', $pm->getEvents());
    $this->assertArrayHasKey('Response', $pm->getEvents());
    $this->assertArrayHasKey('Dispatch', $pm->getEvents());
    $this->assertArrayHasKey('Foo', $pm->getEvents());
}

 

The test is actually in place to see that the Foo event passed into Application::init() gets merged with the default events Response, Request etc etc.

 

Its not that merging that is generating the error either. I'm trying to assign an array to the 'options' key via array_merge, its (apparently) the second argument to array_merge which is not an array. Well, by all accounts, it is!

 

Pretty hard to explain really when this is only a tiny part of a much larger piece.

 

Oh, and to TeddyKiller. there is nothing syntactically incorrect with the code. I only posted what I considered to be relevant.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.