Php 编辑父变量
因此,我试图从子类编辑属性Php 编辑父变量,php,laravel,oop,laravel-5.2,Php,Laravel,Oop,Laravel 5.2,因此,我试图从子类编辑属性category,但由于某些原因,我返回了一个错误。我知道这是为什么,因为需要有两个参数,但其中一个是在de parent类中设置的allready 代码: 孩子 class RestaurantController extends CompanyController { public function __construct(){ parent::__construct(null, "restaurant"); //$this-
category
,但由于某些原因,我返回了一个错误。我知道这是为什么,因为需要有两个参数,但其中一个是在de parent类中设置的allready
代码:
孩子
class RestaurantController extends CompanyController
{
public function __construct(){
parent::__construct(null, "restaurant");
//$this->category = "restaurant";
}
public function getCompany($slug){
$company = parent::index($slug);
return view("restaurant.profile")->withInformation($company);
}
}
父母
class CompanyController extends Controller
{
protected $company;
public $category;
public function __construct(CompanyRepository $company, $category = '')
{
$this->category = $category;
$this->company = $company;
}
public function index($slug)
{
$company = $this->company->getCompany($this->category, $slug);
return compact('company');
}
}
use App\Repositories\CompanyRepository;
class CompanyController extends Controller
{
protected $company;
public function __construct(CompanyRepository $company)
{
$this->company = $company;
}
public function index($slug)
{
$company = $this->company->getCompany($slug);
return compact('company');
}
}
现在我需要知道如何解决这个问题
Edit1
我犯的错误
类型错误:传递给App\Http\Controllers\CompanyController::\uu construct()的参数1必须是App\Repositories\CompanyRepository的实例,给定null,在第16行的/var/www/atify.info/dev-system/App/Http/Controllers/RestaurantController.php中调用
edit2
这个孩子
class RestaurantController extends CompanyController
{
public function getCompany($slug){
$company = parent::index($slug);
return view("restaurant.profile")->withInformation($company);
}
}
父母
class CompanyController extends Controller
{
protected $company;
public $category;
public function __construct(CompanyRepository $company, $category = '')
{
$this->category = $category;
$this->company = $company;
}
public function index($slug)
{
$company = $this->company->getCompany($this->category, $slug);
return compact('company');
}
}
use App\Repositories\CompanyRepository;
class CompanyController extends Controller
{
protected $company;
public function __construct(CompanyRepository $company)
{
$this->company = $company;
}
public function index($slug)
{
$company = $this->company->getCompany($slug);
return compact('company');
}
}
因此,我需要一个类别(用于额外检查。否则,您可能会在另一个具有错误函数的子级中检索公司),因为我有许多子级,每个子级都有特殊函数我认为这是您想要的子级:
class RestaurantController extends CompanyController
{
public $category = 'restuarant';
public function __construct(CompanyRepository $company){
parent::__construct($company, $this->category);
}
public function getCompany($slug){
$company = parent::index($slug);
return view("restaurant.profile")->withInformation($company);
}
}
如果这就是您在构造函数中所做的一切,那么您可以删除子构造函数,并像这样更改父构造函数
public function __construct(CompanyRepository $company, $category = null)
{
if( $category ){
$this->category = $category;
}
$this->company = $company;
}
然后每个子类只设置category属性
class ChildController extends CompanyController
{
public $category = 'Child';
}
请包括您得到的错误。@jfadich已编辑!您正在将一个
null
作为第一个参数传递给parent::u构造(null,“restaurant”)
但是父方法要求您传递一个CompanyRepository
对象作为第一个参数。。。。您需要输入一个CompanyRepository
对象来传递,而不是该对象null@MarkBaker是的,我知道null中断,但是CompanyRepository
是在CompanyController
中设置的。难道没有什么东西可以忽略第一个论点吗。像默认值这样的东西?好吧,这很有效!但是现在,每次我调用extend theCompanyController
时,我都必须将其添加到构造函数中,对吗?不是真的,您可以设置类别并在父构造函数中处理它(我将更新答案)