Answer the question
In order to leave comments, you need to log in
What might cause the PHP Uncaught Error: Using $this when not in object context error?
Good afternoon, I'm writing my own "framework" (I call it that for myself, but in fact it's just self-written garbage, I just write purely for myself to practice). And I ran into a problem that I can not understand.
I have class App. which is the parent of custom classes and methods, it looks like this
<?php
namespace App;
use Symfony\Component\Yaml\Yaml;
define('BASE_DIR', __DIR__ . '/../');
class App
{
/**
* @var Router
*/
public $router;
/**
* App constructor.
*/
public function __construct()
{
$this->router = new Router();
}
public final function run(){
$this->router->match();
}
/**
* @param string $view
* @param array|null $parameters
* @return string
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Runtime
* @throws \Twig_Error_Syntax
*/
public function render(string $view, array $parameters = null) {
$twig = new Twig();
return $twig->render($view, $parameters);
}
/**
* @param string $parameter
* @return mixed
*/
public function getParameter(string $parameter) {
$file = Yaml::parseFile(BASE_DIR . 'config/config.yml');
$parameters = $file['parameters'];
return $parameters[$parameter];
}
}
<?php
namespace Controller;
use App\App;
class AppController extends App
{
public function indexAction() {
return $this->render('index.twig', [
'foo' => 'bar'
]);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question