echo doesn't work in widget

I have this widget:




namespace abc\xyz;


use yii\base\Widget;


class MyWidget extends Widget {

	public function run() {

		echo "hello";

	}

}

Which I call in this way:




use abc\xyz\MyWidget;


MyWidget::widget(array());



However, the "hello" is not printed, but no error messages!! And I am sure the run() method is executed.

I found the solution! It’s weird, I had to write echo like this:


echo MyWidget::widget(array());

In Yii 1 this echo was not required.

Yup.

Yii 1:




        public function widget($className,$properties=array(),$captureOutput=false)

	{

		if($captureOutput)

		{

			ob_start();

			ob_implicit_flush(false);

			try

			{

				$widget=$this->createWidget($className,$properties);

				$widget->run();

			}

			catch(Exception $e)

			{

				ob_end_clean();

				throw $e;

			}

			return ob_get_clean();

		}

		else

		{

			$widget=$this->createWidget($className,$properties);

			$widget->run();

			return $widget;

		}

	}



But in Yii 2:




    public static function widget($config = [])

    {

        ob_start();

        ob_implicit_flush(false);

        /* @var $widget Widget */

        $config['class'] = get_called_class();

        $widget = Yii::createObject($config);

        $out = $widget->run();


        return ob_get_clean() . $out;

    }



great explanation, Bizley