I becmoe this error
Fatal error: Uncaught Error: Call to undefined method Closure::index() in C:\xampp\htdocs\projects\mBv\public\index.php:45
Stack trace: #0 {main} thrown in C:\xampp\htdocs\projects\mBv\public\index.php on line 45
I have a Container
class, that includes all other classes as array in its __construct
, which I need to use it.
Container.php
<?php
namespace App\Core;
use PDO;
use Exception;
use PDOException;
use App\Post\Container\PostContainer;
use App\User\Container\UserContainer;
use App\Post\Repository\PostsRepository;
use App\Post\Repository\CommentsRepository;
class Container
{
private $receipts = [];
private $instances = [];
public function __construct()
{
$this->receipts = [
'postsRepository' => function() {
$postContainer= $this->make('postContainer');
$postsRepository= $postContainer->postReceipts['postsRepository'];
return $postsRepository;
},
'commentsRepository' => function() {
$postContainer= $this->make('postContainer');
$commentsRepository= $postContainer->postReceipts['commentsRepository'];
return $commentsRepository;
},
'pdo' => function() {
try {
$pdo = new PDO(
'mysql:host=XXXX;dbname=XXXX;charset=utf8',
'XXXX',
'XXXX'
);
} catch (PDOException $e) {
echo "ERROR";
die();
}
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
return $pdo;
}
];
}
public function make($name)
{
if (!empty($this->instances[$name]))
{
return $this->instances[$name];
}
if (isset($this->receipts[$name])) {
$this->instances[$name] = $this->receipts[$name]();
}
return $this->instances[$name];
}
}
?>
i made for the Post, its own Container, which include the comments and post repository.
The reason for that it's when I want to change the structure of the classes later (comments or post) then i have to change it just from their own container. So their own Container of post and comments seems like this
PostContainer.php
<?php
namespace App\Post\Container;
use App\Post\Repository\PostsRepository;
use App\Post\Repository\CommentsRepository;
//admin - postsController - post and comments Repository
class PostContainer
{
public $postReceipts = [];
public function __construct() {
$this->postReceipts = [
'postsRepository' => function() {
return new PostsRepository(
$this->make("pdo")
);
},
'commentsRepository' => function() {
return new CommentsRepository(
$this->make("pdo")
);
},
];
}
}
?>
when i run it i become the error, that I already mentioned it at the beginning.
Here ist the index file, where the error is in the line 45.
<?php
session_start();
require __DIR__ . "/../init.php";
$pathInfo = $_SERVER['PATH_INFO'];
$routes = [
'/index' => [
'controller' => 'postsController',
'method' => 'index'
],
'/post' => [
'controller' => 'postsController',
'method' => 'show'
]
];
if (isset($routes[$pathInfo])) {
$route = $routes[$pathInfo];
$controller = $container->make($route['controller']);
$method = $route['method'];
$controller->$method(); // the error place is in this line
}
?>
here is the init file, which includes the $container init.php
<?php
require __DIR__ . "/autoload.php";
function ePreventXss($str)
{
return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
}
$container = new App\Core\Container();
?>
postController.php
<?php
namespace App\Post\Controller;
use App\Core\Controller\AbstractController;
use App\Post\Repository\PostsRepository;
use App\Post\Repository\CommentsRepository;
class PostsController extends AbstractController
{
public function __construct(PostsRepository $postsRepository,CommentsRepository $commentsrepository)
{
$this->postsRepository = $postsRepository;
$this->commentsrepository = $commentsrepository;
}
public function index()
{
$posts = $this->postsRepository->showPostsIndex();
$this->render("post/index", [
'posts' => $posts
]);
}
public function show()
{
$postId= $_GET['id'];
$this->deleteComments($postId);
$this->setkommentar($postId);
$this->getAllComments($postId);
}
private function getAllComments($postId)
{
$post= $this->postsRepository->loadPostById($postId);
$comments= $this->commentsrepository->getAllCommentsByPost($postId);
$this->render("post/show",[
'post'=>$post,
'comments'=>$comments
]);
}
private function setkommentar($postId)
{
if(isset($_POST['content']))
{
$contentKommentar= $_POST['content'];
$this->commentsrepository->insertKommentar($postId,$contentKommentar);
}
}
public function deleteComments($postId)
{
if(isset($_POST['commentId']))
{
$commentId= $_POST['commentId'];
$this->commentsrepository->loadKommentarToDelete($postId, $commentId);
}
}
}
?>
i invoke the code from PostContainer into the Container class as you can see in the examples , so I use the Container class to produce the obejcts of the post and comment repository.
I tried already to solve it, unfortunately i did't can.
I read that sometime this error will happen, when the version of php less than 7, my php version is: 8.0.2