Skip to content
Advertisement

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:xampphtdocshermesmvccoreapp.php on line 40 and exactly 1 expected in C:xampphtdocshermesmvccontrollsproduct.php:14 Stack trace: #0 C:xampphtdocshermesmvccoreapp.php(40): product->index() #1 C:xampphtdocshermesmvcindex.php(9): App->__construct() #2 {main} thrown in C:xampphtdocshermesmvccontrollsproduct.php on line 14

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement