Dependency Injection to few arguments - php

i dont really understand why i get this Error Message and i hope you guys can help:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function App\Controller\Home::__construct(), 0 passed in /mnt/c/mvc/index.php on line 25 and exactly 1 expected in /mnt/c/mvc/src/Controller/Home.php:12 Stack trace: #0 /mnt/c/mvc/index.php(25): App\Controlyler\Home->__construct() #1 {main} thrown in /mnt/c/mvc/src/Controller/Home.php on line 12
index.php
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
use App\Core\{Router, Request};
use App\Service\View;
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/src/Core/Bootstrap.php';
$routerLoad = Router::load('src/core/Routes.php')
->direct(Request::uri());
$routerAction = new $routerLoad;
$routerAction->action();
$view = new View();
$view->display();
Home.php
namespace App\Controller;
use App\Service\ViewInterface;
use App\Service\View;
class Home implements Controller
{
private ViewInterface $view;
public function __construct(View $view)
{
$this->view = $view;
}
public function action(): void
{
$this->view->addTemplate('layout.tpl');
$this->view->addTlpParam('headline', 'Home');
$this->view->addTlpParam('info', 'Welcome');
$this->view->addTlpParam('name', 'User');
$this->view->display();
}
}
View.php
namespace App\Service;
class View implements ViewInterface
{
private \Smarty $smarty;
private string $template;
public function __construct()
{
$this->smarty = new \Smarty();
$this->smarty->setTemplateDir('/mnt/c/mvc/src/View/templates');
$this->smarty->setCompileDir('/mnt/c/mvc/src/smarty/templates_c');
$this->smarty->setCacheDir('/mnt/c/mvc/src/smarty/cache');
$this->smarty->setConfigDir('/mnt/c/mvc/src/smarty/configs');
}
public function addTemplate(string $template):void
{
$this->template = $template;
}
public function addTlpParam(string $name, $value): void
{
$this->smarty->assign($name, $value);
}
public function display(): void
{
try {
$this->smarty->display($this->template);
} catch (\SmartyException $e) {
} catch (\Exception $e) {
}
}
}

Related

Fatal error: Uncaught Error: Non-static method primaryKey() cannot be called statically [duplicate]

This question already has answers here:
Error message Strict standards: Non-static method should not be called statically in php
(7 answers)
Closed 1 year ago.
how can I fix this error?
Fatal error: Uncaught Error: Non-static method thecodeholic\phpmvc\db\DbModel::primaryKey() cannot be called statically in C:\xampp\htdocs\php-mvc-framework-master\vendor\thecodeholic\php-mvc-core\Application.php:53 Stack trace: #0 C:\xampp\htdocs\php-mvc-framework-master\public\index.php(25): thecodeholic\phpmvc\Application->__construct('C:\\xampp\\htdocs...', Array) #1 {main} thrown in C:\xampp\htdocs\php-mvc-framework-master\vendor\thecodeholic\php-mvc-core\Application.php on line 53
here is my index.php in public folder:
<?php
/**
* User: TheCodeholic
* Date: 7/7/2020
* Time: 9:57 AM
*/
use app\controllers\AboutController;
use app\controllers\SiteController;
use thecodeholic\phpmvc\Application;
require_once __DIR__ . '/../vendor/autoload.php';
$dotenv = \Dotenv\Dotenv::createImmutable(dirname(__DIR__));
$dotenv->load();
$config = [
'userClass' => \app\models\User::class,
'db' => [
'dsn' => $_ENV['DB_DSN'],
'user' => $_ENV['DB_USER'],
'password' => $_ENV['DB_PASSWORD'],
]
];
$app = new Application(dirname(__DIR__), $config);
$app->on(Application::EVENT_BEFORE_REQUEST, function(){
echo "Before request from second installation";
});
$app->router->get('/', [SiteController::class, 'home']);
...
$app->run();
and here is Application.php code:
<?php
/**
* User: TheCodeholic
* Date: 7/7/2020
* Time: 9:57 AM
*/
namespace thecodeholic\phpmvc;
use thecodeholic\phpmvc\db\Database;
/**
* Class Application
*
* #author Zura Sekhniashvili <zurasekhniashvili#gmail.com>
* #package app
*/
class Application
{
const EVENT_BEFORE_REQUEST = 'beforeRequest';
const EVENT_AFTER_REQUEST = 'afterRequest';
protected array $eventListeners = [];
public static Application $app;
public static string $ROOT_DIR;
public string $userClass;
public string $layout = 'main';
public Router $router;
public Request $request;
public Response $response;
public ?Controller $controller = null;
public Database $db;
public Session $session;
public View $view;
public ?UserModel $user;
public function __construct($rootDir, $config)
{
$this->user = null;
$this->userClass = $config['userClass'];
self::$ROOT_DIR = $rootDir;
self::$app = $this;
$this->request = new Request();
$this->response = new Response();
$this->router = new Router($this->request, $this->response);
$this->db = new Database($config['db']);
$this->session = new Session();
$this->view = new View();
$userId = Application::$app->session->get('user');
if ($userId) {
$key = $this->userClass::primaryKey();
$this->user = $this->userClass::findOne([$key => $userId]);
}
}
public static function isGuest()
{
return !self::$app->user;
}
public function login(UserModel $user)
{
$this->user = $user;
$primaryKey = $user->primaryKey();
$value = $user->{$primaryKey};
Application::$app->session->set('user', $value);
return true;
}
public function logout()
{
$this->user = null;
self::$app->session->remove('user');
}
public function run()
{
$this->triggerEvent(self::EVENT_BEFORE_REQUEST);
try {
echo $this->router->resolve();
} catch (\Exception $e) {
echo $this->router->renderView('_error', [
'exception' => $e,
]);
}
}
public function triggerEvent($eventName)
{
$callbacks = $this->eventListeners[$eventName] ?? [];
foreach ($callbacks as $callback) {
call_user_func($callback);
}
}
public function on($eventName, $callback)
{
$this->eventListeners[$eventName][] = $callback;
}
}
$key = $this->userClass->primaryKey();
$this->user = $this->userClass->findOne([$key => $userId]);
i think the function primaryKey and findOne will be normal public and not static in your Entity.
class UserClass {
public function primaryKey() {
....
}
public function findOne() {
...
}
}
so you has to use -> instead of :: to call the methods

Too few arguments to function php

my controller
class product extends Controller
{
function __construct()
{
}
public function index($id)
{
$productInfo = $this->model->productInfo($id);
print_r($productInfo);
$this->view('product/index.php');
}
}
?>
my model
class model_product extends Model
{
function __construct()
{
parent::__construct();
}
function productInfo($id)
{
$sql = 'select * from tbl_product where id=:x ';
$stmt = self::$conn->prepare($sql);
$stmt->bindParam(':x', $id);
$stmt->execute();
$result = $stmt->fetch();
return $result;
}
}
app.php
class App{
public $controller='index';
public $method='index';
public $params= [];
function __construct()
{
if(isset($_GET['url'])){
$url=$_GET['url'];
$url=$this->parseUrl($url);
$this->controller=$url[0];
unset($url[0]);
if (isset($url[1]))
{
$this->method=$url[1];
unset($url[1]);
}
$params=array_values($url);
}
$controllerUrl='controlls/'.$this->controller. '.php.';
if (file_exists($controllerUrl)){
require ($controllerUrl);
$object=new $this->controller;
$object->model($this->controller);
if(method_exists($object,$this->method))
call_user_func_array([$object,$this->method],$this->params);
}
}
function parseUrl($url){
$url=filter_var($url,FILTER_SANITIZE_URL);
$url=rtrim($url,'/');
$url=explode('/',$url);
return $url;
}
it shows this error
Fatal error: Uncaught ArgumentCountError: Too few arguments to function product::index(), 0 passed in C:\xampp\htdocs\hermesmvc\core\app.php on line 40 and exactly 1 expected in C:\xampp\htdocs\hermesmvc\controlls\product.php:14 Stack trace: #0 C:\xampp\htdocs\hermesmvc\core\app.php(40): product->index() #1 C:\xampp\htdocs\hermesmvc\index.php(9): App->__construct() #2 {main} thrown in C:\xampp\htdocs\hermesmvc\controlls\product.php on line 14
Replace in your app.php $params=array_values($url); by $this->params=array_values($url);
Because if you dont set $this->params In construtor, it will stay empty. Quite like what you made for method and controller.

Require has failed in php

I'm new to PHP, I'm trying to require UserController.php from Controller.php but all I get is "HTTP ERROR 500" in browser. What's going on here?
Controller.php
class Controller
{
public function __construct()
{
}
public function call(){
// echo 1;
require_once "../Controllers/UserController.php";
}
}
UserController.php
class UserController
{
public function __construct()
{
echo '111111111';
}
public function hi(){
echo '1';
}
}
$a = new UserController();
$a->hi();
Class definitions can't be nested inside functions or other classes. So you shouldn't have that require_once line inside a function definition. Move it outside the class.
require_once "../Controllers/UserController.php";
class Controller
{
public function __construct()
{
}
public function call(){
// echo 1;
}
}
<?php
require_once "../Controllers/UserController.php";
class Controller
{
public function __construct()
{
}
public function call(){
// echo 1;
$a = new UserController();
$a->hi();
}
}

Fatal Error - Call to undefined method "Customersss::throwError()"

I created a class and try to call in a method, but however in my error log, I am getting response "Uncaught Error: Call to undefined method Customersss::throwError()", please what am I doing wrong, because I know I have created throwError(), I can't seem to be able to access the class.
Firstly the class trying to call object
$this->throwError(INVALID_DATA_TTT, "Invalid shipping fee"); //WHERE I SUSPECT ERROR IS BEING GENERATED
The full error
PHP Fatal error: Uncaught Error: Call to undefined method
Customersss::throwError() in
/home/osconliz/public_html/Osconlizapicall/customers.php:276 Stack
trace:
#0 /home/osconliz/public_html/Osconlizapicall/api.php(177): Customersss->insertNewDelivery()
#1 [internal function]: Api->create_insert_new_delivery()
#2 /home/osconliz/public_html/Osconlizapicall/rest.php(42): ReflectionMethod->invoke(Object(Api))
#3 /home/osconliz/public_html/Osconlizapicall/index.php(4): Rest->processApi()
#4 {main} thrown in /home/osconliz/public_html/Osconlizapicall/customers.php on line 276
customers.php
require_once('constants.php');
require_once('rest.php');
class Customersss {
private $id;
private $shipping_fee;
private $pickup_fee;
function setId($id){ $this->id = $id; }
function getId() { return $this->id; }
function setShipping_fee($shipping_fee){ $this->shipping_fee = $shipping_fee; }
function getShipping_fee() { return $this->shipping_fee; }
function setPickup_fee($pickup_fee){ $this->pickup_fee = $pickup_fee; }
function getPickup_fee() { return $this->pickup_fee; }
public function __construct(){
$db = new DbConnect();
$this->dbConn = $db->connect();
}
public function insertNewDelivery(){
if ($this->shipping_fee == ""){
$this->throwError(EMPTY_PARAMETER, "Empty shipping fee");
exit();
}
if ($this->shipping_fee == ""){
$this->throwError(INVALID_DATA_TTT, "Invalid shipping fee");
exit();
}
}
}
rest.php
require_once('constants.php');
class Rest {
protected $request;
protected $serviceName;
protected $param;
public function processApi(){
$api = new API;
$rMethod = new reflectionMethod('API', $this->serviceName);
if(!method_exists($api, $this->serviceName)){
$this->throwError(API_DOST_NOT_EXIST, "API does not exist");
}
$rMethod->invoke($api);
}
public function throwError($code, $message){
header("content-type: application/json");
$errorMsg = json_encode(['error' => ['status'=>$code, 'message'=>$message]]);
echo $errorMsg; exit;
}
}
constants.php
define('INVALID_DATA_TTT', 350);
define('EMPTY_PARAMETER', 404);
define('API_DOST_NOT_EXIST', 400);
define('ACCESS_TOKEN_ERRORS', 500);
api.php
require_once "customers.php";
require_once "constants.php";
class Api extends Rest {
public $dbConn;
public function __construct(){
parent::__construct();
$db = new DbConnect;
$this->dbConn = $db->connect();
}
public function create_insert_new_delivery(){
$shipping_fee= $this->validateParameter('item_category', $this->param['shipping_fee'], STRING, true);
try {
$cust = new Customersss;
} catch (Exception $e){
$this->throwError(ACCESS_TOKEN_ERRORS, $e->getMessage());
}
}
You are calling Rest::ThrowError() from Customersss class. It means your function is unreachable in this context.
To call this Rest method from the Customersss class, you can:
extend Rest to Customersss (see inheritance)
make ThrowError() a static method (see static)
Using inheritance:
class Customersss extends Rest { /* your properties/methods */ }
Using a static method:
class Rest {
static public function throwError($code, $message){ /* your code */ }
}
Callable with Rest::ThrowError()

Class autoload not working

I'm a beginner in PHP development and I'm facing a problem in my development in PHP OO. I saw is better use the autoload() function than include each file of PHP Class.
My doubt is: Why my autoload function does not work?
Follow bellow my code:
<?php
function __autoload($class)
{
include_once "model/{$class}.class.php";
}
$avaliacaoLocal = new AvaliacaoLocal();
$avaliacaoLocal->setId(1);
$avaliacaoLocal->setIdLocal(2);
$avaliacaoLocal->setComentarios("Comentários de Pedro");
$avaliacaoLocal->setIdPessoaCliente(3);
$avaliacaoLocal->setValor(5);
var_dump($avaliacaoLocal);
File AvaliacaoLocal.class.php
<?php
namespace model;
class AvaliacaoLocal
{
private $id;
private $valor;
private $comentarios;
private $idLocal;
private $idPessoaCliente;
public function __construct(){
$this->clear();
}
public function clear(){
$this->id = 0;
$this->valor = 0;
$this->comentarios = "";
$this->idLocal = null;
$this->idPessoaCliente = null;
}
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function getValor()
{
return $this->valor;
}
public function setValor($valor)
{
$this->valor = $valor;
}
public function getComentarios()
{
return $this->comentarios;
}
public function setComentarios($comentarios)
{
$this->comentarios = $comentarios;
}
public function getIdLocal()
{
return $this->idLocal;
}
public function setIdLocal($idLocal)
{
$this->idLocal = $idLocal;
}
public function getIdPessoaCliente()
{
return $this->idPessoaCliente;
}
public function setIdPessoaCliente($idPessoaCliente)
{
$this->idPessoaCliente = $idPessoaCliente;
}
}
The error:
PHP Fatal error: Class 'AvaliacaoLocal' not found in C:\Users\Pedro
........\index.php on line 14
UPDATE:
When i use include the PHP returns the same error:
Fatal error: Class 'AvaliacaoLocal' not found in C:\Program
Files\VertrigoServ\www\system\index.php on line 10
i've change folder to verify if could be it.
The class is declared belonging to a namespace, you have to call it in this way:
$avaliacaoLocal = new \model\AvaliacaoLocal();
But now, the namespace is also included in $class, so the autoload function needs to handle that:
function __autoload($class)
{
$file = str_replace(array('_', '\\'), '/', $class) . '.php';
if (is_file($file)) {
require $file;
}
}
This function takes $class value and replace every \ (and _) from the namespace with a / to get the file name.

Categories