Как при расширении класса в PHP сделать все параметры необязательными?Php

Кемеровские программисты php общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Как при расширении класса в PHP сделать все параметры необязательными?

Сообщение Anonymous »


Specifically, I'm having trouble importing interfaces into class methods.

I'm creating a PHP framework for in-house use, and like most frameworks, there is a base controller that all controllers extend.

Inside the base controller, there is a portion of code that executes the requested class method:

# Here the controller is calling the method. Note that it is not passing any parameters $__instantiate_class->{$this->action}(); Here's an example of how a child class that extends the above base controller is created:

namespace App\Controller; use \Src\Controller\Base_Controller; use \Src\Middleware\EmailHelper; use \Symfony\Component\Mailer\MailerInterface; class Home_Controller extends Base_Controller { public function index() { // Do something } } This works out perfectly fine, just as expected. So here comes the problem.

I'm trying to integrate the Symfony Mailer library. The issue is that it requires you to import the MailerInterface interface (as shown above). I cannot simply pass the interface as a parameter, else I get an exception (missing parameter) because the base controller does not pass any parameters:

// Does NOT work, throws exception public function index( MailerInterface $mailer ){} // because in parent controller, no params are passed $__instantiate_class->{$this->action}(); I know I could simply pass a default param in the base controller's method call:

$__instantiate_class->{$this->action}( $args = null); But this isn't a good solution either, since a child class may need multiple parameters (or none).

I don't want to have the Home_Controller (or any controller extending Base_Controller) implementing the interface, because 1. I'm not sure it would even work and 2. it would bloat the class with unneeded methods and 3. simplicity's sake

I don't mind rewriting a bit of code if necessary, I just need a way to pass as many or few parameters as needed to children classes.


Источник: https://stackoverflow.com/questions/781 ... s-optional
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Php»