Im trying to run my class with the container but when i execute it i get
Fatal error: Uncaught RuntimeException: Unexpected data in output buffer. Maybe you have characters before an opening <?php tag? in /srv/www/vendor/slim/slim/Slim/App.php:588 Stack trace: #0 /srv/www/vendor/slim/slim/Slim/App.php(377): Slim\App->finalize(Object(Slim\Http\Response)) #1 /srv/www/vendor/slim/slim/Slim/App.php(295): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response)) #2 /srv/www/public/index.php(3): Slim\App->run() #3 {main} thrown in /srv/www/vendor/slim/slim/Slim/App.php on line 588
Maybe im doing this wrong, tell me if so
Added before $app->run();
$container[‘CronModel’]->add(‘foobar’);
function foobar() {
echo ‘im the foobar function’;
}
$container['CronModel']->execute();
Add to the container
$container[‘CronModel’] = function ($container) {
return new \App\Models\CronModel($container);
};
CronModel.php
<?php
namespace App\Models;
class CronModel
{
protected $container;
protected $functions = [];
public function __construct($container)
{
$this->container = $container;
}
public function add($function, $args = [])
{
if (!is_numeric($function) and !is_integer($function)) {
if (is_string($function) or is_array($function)) {
$this->functions[] = [
'name' => $function,
'args' => $args
];
}else{
die('given function ('.$function.') must be an array or string');
}
}else{
die('given function ('.$function.') must be an array or string');
}
}
public function execute()
{
dump( $this->functions );
foreach ($this->functions as $key => $function) {
if (!empty($function['args'])) {
call_user_func_array($function['name'], $function['args']);
}else{
call_user_func($function['name']);
}
}
}
}
?>