Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 致命错误:未捕获错误:初始化之前不能访问类型化属性_Php_Asp.net Mvc - Fatal编程技术网

Php 致命错误:未捕获错误:初始化之前不能访问类型化属性

Php 致命错误:未捕获错误:初始化之前不能访问类型化属性,php,asp.net-mvc,Php,Asp.net Mvc,我不明白为什么我会收到这个错误消息: 致命错误:未捕获错误:类型化属性App\Controller\products::$ProductRepository在/mnt/c/mvc/src/Controller/products.php:24堆栈跟踪:#0/mnt/c/mvc/index.php(21):App\Controller\products->action()#1{main}在第24行的/mnt/c/mvc/src/Controller/products.php中抛出 我希望有人能解释为

我不明白为什么我会收到这个错误消息:

致命错误:未捕获错误:类型化属性App\Controller\products::$ProductRepository在/mnt/c/mvc/src/Controller/products.php:24堆栈跟踪:#0/mnt/c/mvc/index.php(21):App\Controller\products->action()#1{main}在第24行的/mnt/c/mvc/src/Controller/products.php中抛出

我希望有人能解释为什么会抛出此错误,提前谢谢

ProductRepository.php

class ProductRepository
{
   private array $ProductList;

   public function getList(): array
   {
       $productJson = file_get_contents('model.json');

       $decodedProductList = json_decode($productJson, true);

       return explode(', ', $decodedProductList);
}
products.php

class products extends PageController
{
   private ProductRepository $ProductRepository;

   public function action(): void
   {
       $this->smarty->assign('headline', 'PRODUCTS');
       $this->smarty->assign('info', 'Product Overview');
       $this->smarty->assign('name', 'Every Product!');
       $this->smarty->assign('LIST', $this->ProductRepository->getList());

       try {
           $this->smarty->display('products.tpl');
       } catch (\SmartyException $e) {
       } catch (\Exception $e) {
       }
   }
}
我试图从JSON文件中获取一个数组,我不确定这是否是正确的方法,但我的主要问题是这条错误消息

我希望你们中的一些人能告诉我搜索的方向。
提前谢谢

要从
class ProductRepository
访问方法
getList()
,您需要创建一个对象。在
类产品的构造函数中执行此操作

<?php
class products extends PageController
{
    private ProductRepository $ProductRepository;

    public function __construct()
    {
        $this->ProductRepository = new ProductRepository(); // you now have access to the public methods of ProductRepository
    }

    public function action(): void
    {
        $this->smarty->assign('headline', 'PRODUCTS');
        $this->smarty->assign('info', 'Product Overview');
        $this->smarty->assign('name', 'Every Product!');
        $this->smarty->assign('LIST', $this->ProductRepository->getList());

        try {
            $this->smarty->display('products.tpl');
        } catch (\SmartyException $e) {
        } catch (\Exception $e) {
        }
    }
}

您实际在哪里设置
$ProductRepository
属性?除非在该属性中存储对象,否则将无法将其用作对象。用
ProductRepository
键入提示不会创建实际实例。我不明白你用set是什么意思?它是我从JSON文件中获取数据的类的名称。如何创建它的实际实例?您是否尝试过明显的选择,即new ProductRepository()?您必须先设置
$ProductRepository
属性,然后才能将其与
$this->ProductRepository
一起使用。在构造函数中用ProductRepository类的实例设置属性似乎是一种方法。maan也许我应该睡觉了。。。非常感谢。很高兴我能帮忙!