Fatal error: Uncaught Error: Call to undefined method Closure::index() php
Get the solution ↓↓↓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 aContainer
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
Answer
Solution:
that is the answer, the problem was that i had in the PostContainer no arrays one to save the objects in it and the second two check and to make certain that no objects will be returned twice in the same array.
public $receipts = []; public $instances = [];
and I had either to write the make function in the two classes PostContainer and the Container or to let the classes to extend it from an abstract class, but i choosed the simplest way, which is to write the make() in both.
In addition i had to make a function, that make an object of the PostContainer class in the Container class, in order to have access to the calss and its elements and methods.
So the problem was just in (PostContainer - Contianer) classes. And as @steven7mwesigwa suggested, that i should to use IIFEs( Immediately Invoked Function Expressions).– I used it in the Container class, without IIFEs the code it's not able to be executed.
I hope, that i could to explain my answer clearly.
When you have questions about my situation, you are welcome.
so the final answer for the question as the following:
PostContainer.php
<?php
namespace App\Post\Container;
use PDO;
use Exception;
use PDOException;
use App\Post\Repository\PostsRepository;
use App\Post\Repository\CommentsRepository;
use App\Post\Controller\PostsController;
use App\Core\Container\AbstractContainer;
class PostContainer
{
public $receipts = [];
public $instances = [];
public function __construct()
{
$this->receipts=[
"postsController" => function (){
return new PostsController(
$this->make('postsRepository'),
$this->make('commentsRepository')
);
} ,
"commentsRepository" => function (){
return new CommentsRepository(
$this->make('pdo')
);
} ,
"postsRepository" => function (){
return new PostsRepository(
$this->make('pdo')
);
} ,
'pdo' => function() {
try {
$pdo = new PDO(
'mysql:host=localhost;dbname=XXXX;charset=utf8',
'XXXXX',
'XXXXX'
);
} catch (PDOException $e) {
echo "Verbindung zur Datenbank fehlgeschlagen";
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];
}
}
?>
Container.php
<?php
namespace App\Core;
use App\Post\Container\PostContainer;
use App\Core\Container\AbstractContainer;
class Container
{
private $receipts = [];
private $instances = [];
private $postContainer;
public function __construct()
{
$this->receipts=[
"postsRepository" => (function(){
return $this->makePostContainer()->receipts['postsRepository'];
})(),
"commentsrepository" => (function(){
return $this->makePostContainer()->receipts['commentsRepository'];
})(),
"postsController" => (function(){
return $this->makePostContainer()->receipts['postsController'];
})(),
"pdo" => (function(){
return $this->makePostContainer()->receipts['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];
}
private function makePostContainer()
{
$this->postContainer= new PostContainer;
return $this->postContainer;
}
}
?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: string literal contains an unescaped line break
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.