Yii2: PHP Warning – yii\base\ErrorException Expected array for frame 0

Created on 15 Mar 2016  Â·  27Comments  Â·  Source: yiisoft/yii2

Problem with ErrorHandler.
Confirmed for PHP 7.0.4, yii 2.0.7, CLI (yiiconsoleApplication), YII_DEBUG enabled, xdebug enabled.

Reproducing:

  • cause any warning inside console controller
$a = [];
$b = $a['ntf'];
  • call CLI action ./yii controller/action with warning
  • you'll see stack of errors
PHP Warning:  Expected array for frame 0 in /.../vendor/yiisoft/yii2/console/ErrorHandler.php on line 49
PHP Stack trace:
PHP   1. yii\base\ErrorHandler->handleException() /.../vendor/yiisoft/yii2/base/ErrorHandler.php:0
PHP   2. Exception->__toString() /.../vendor/yiisoft/yii2/base/ErrorHandler.php:120
PHP   3. Exception->getTraceAsString() /.../vendor/yiisoft/yii2/base/ErrorHandler.php:120
PHP   4. yii\base\ErrorHandler->handleError() /.../vendor/yiisoft/yii2/base/ErrorHandler.php:120
PHP   5. yii\base\ErrorHandler->handleException() /.../vendor/yiisoft/yii2/base/ErrorHandler.php:203
PHP   6. yii\console\ErrorHandler->renderException() /.../vendor/yiisoft/yii2/base/ErrorHandler.php:109
PHP   7. Exception->getTraceAsString() /.../vendor/yiisoft/yii2/console/ErrorHandler.php:49
PHP Warning 'yii\base\ErrorException' with message 'Expected array for frame 0'

in /.../vendor/yiisoft/yii2/base/ErrorHandler.php:120

Switched to custom ErrorHandler using ErrorException without xdebug related code fixes problem.
Look at yiibaseErrorException:45

php7

Most helpful comment

You can delete files in runtime

All 27 comments

It's a heisenbug :fearful:

I failed to reproduce it in the described way, however I spectate this error in my project on local env sometimes. Usually it happens just after system load and I fix it in non-obvious way like page refresh or disabling/enabling X-Debug listener in PHPStorm. I have no idea why does it happen :(

I receive this error too. With other code. Console application too.
PHP 7.0 only, PHP 5.6 ok.

I failed to reproduce it in the described way, however I spectate this error in my project on local env sometimes.

same here...

+

If anybody have confirmed way to reproduce the error - please, share it!

Php 7.0.0, xdebug enabled:
for web app
in index.php: defined('YII_ENV_TEST') or define('YII_ENV_TEST', true); (to pass in branch with output as string, or use ajax request to get action).

in controller, before class

interface A {public static function a();}
class B implements A{};

in action

Yii::$app->response->format = Response::FORMAT_HTML;
new B();

get this action and you will see error PHP Warning 'yii\base\ErrorException' with message 'Expected array for frame 0.

After deleting $exception->getTraceAsString() in
https://github.com/yiisoft/yii2/blob/master/framework/base/ErrorHandler.php#L321
you will see normal exception with warning Expected array for frame 0 and file and line number, where it warning was (exception to string conversion).

I didn't go deeper. Also I didn't test console commands, but problem the same: converting exception or trace to string.

I think it happens after replacing trace of Exception by trace from XDebug:
https://github.com/yiisoft/yii2/blob/master/framework/base/ErrorException.php#L67

+1

+1

Same situation in console and web application on ErrorException

PHP 7.0.5 + xdebug 2.4.1-dev + Yii 2.0.7

Just to reinforce the problem: we are seeing this PHP Warning: Expected array for frame 0 in the web application sometimes, but only in computers with xdebug enabled.

One example happened in a AJAX call. One of our classes was extending from Model, but the developer forgot to include use yii\base\Model. In her Linux machine with xdebug _enabled_, that generic warning showed up _without the full stack trace_. In my Mac with xdebug _disabled_, I've got the warning Class not found _with the full stack trace_.

Edit: just to make sure it is really about xdebug, we disabled it in her machine and the proper warning showed up. So it is confirmed that Yii2's ErrorException has some problem with xdebug.

You can delete files in runtime

+1

+1

A simple die('anything'); inside any action will cause this error.
This is the my stack trace.

(PHP 7, xdebug enabled, Linux)

Edit:
Now I see the error for everywhere. Even rolling back my code to a known working point. I will check.

+1

I tried the following steps, and the issue is disappeared :

  1. set xdebug.remote_enable=false in ext-xdebug.ini, and restart php-fpm.
  2. reset xdebug.remote_enable=true and restart php-fpm again.

(PHP 7.05, xdebug 2.4.0, PhpStorm)

@jolinp sometimes even fpm restart solves the problem

10 minutes ago, I had the same warning. I tried to disable xdebug and it did not help me. Then I switched to php 5.6.16 and I god error:
Allowed memory size of xxx bytes exhausted (tried to allocate yyy bytes) in php
(I tried to create preview of big image).

I increased memory limit and it solve the problem with memory. Then I switched back to php 7.0.2 and anything is OK.

Maybe it will be helpful.

@astappev when you've switched back did you increase PHP 7 allowed memory as well?

Yes, of course. I think it can be partially related to that

Hi, I had the same error.

My env is:

  • Kubuntu 16.04
  • php 7.0.4-7ubuntu2.1
  • Yii2 2.0.8

When I set chmod 777 -R on .../{backend|frontend}/runtime the problem was solved.

Clear everything inside .../runtime worked to me (at least for now). It was already 777.

My env is PHP 7.0.4-7ubuntu2.1 / xdebug v2.4.0

I could reproduce similar behavior using below script:

<?php
require __DIR__ . '/vendor/autoload.php';
use yii\base\ErrorException;

function handleError($code, $message, $file, $line) {
    throw new ErrorException($message, $code, $code, $file, $line);
}
set_error_handler('handleError');

class Klazz {
    public static function foo() {
        static::bar();
    }
    private static function bar() {
        eval("echo \$foo['bar'];");
    }
}

Klazz::foo();
$ php -v
PHP 7.0.7 (cli) (built: May 25 2016 17:35:28) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
    with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans

$ php xdebugtest.php 
PHP Fatal error:  Uncaught yii\base\ErrorException: Expected array for frame 0 in Unknown:0
Stack trace:
#0 {main}
  thrown in Unknown on line 0

Just when use yii\base\ErrorException; line removed, I could get expected result:

$ php xdebugtest.php 
PHP Fatal error:  Uncaught ErrorException: Undefined variable: foo in /.../xdebugtest.php(22) : eval()'d code:1
Stack trace:
#0 /.../xdebugtest.php(22) : eval()'d code(1): handleError(8, 'Undefined varia...', '/...', 1, Array)
#1 /.../xdebugtest.php(22): eval()
#2 /.../xdebugtest.php(17): Klazz::bar()
#3 /.../xdebugtest.php(26): Klazz::foo()
#4 {main}
  thrown in /.../xdebugtest.php(22) : eval()'d code on line 1

Isn't it closed?

yes, thanks!

this problem may stil reproduce by Yii2
the flowing codes are not included any phpfile and it doesn't throw error

class MyException extends \ErrorException{
    public function __toString(){
        return parent::__toString();
    }
}
$e = new MyException('abc');
echo $e->__toString();

but it's happened when running in Yii2 with calling __toString()

namespace app\controllers;

//you can try to extends \ErrorException,it's no problem
class MyException extends \yii\base\ErrorException{}

class SiteController extends \yii\web\Controller
{
    public function actionMain(){
        $e = new MyException('abc');
        $e->__toString();
    }
}

if MyException extends ErrorException then error not happen,

php(7.0.1)+Yii2(2.0.8)+Xdebug(2.4.0.11)

the fix will be released with 2.0.9, it is expected to not work with 2.0.8

This problem can be a cause of having the name of a function repeated in the model.

This happened to me and solve it by changing the name to the function

PHP Warning – yii\base\ErrorException Expected array for frame 0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

deecode picture deecode  Â·  50Comments

cebe picture cebe  Â·  53Comments

samdark picture samdark  Â·  63Comments

njasm picture njasm  Â·  44Comments

Mirocow picture Mirocow  Â·  56Comments