Как при расширении класса в PHP сделать все параметры необязательными? ⇐ Php
Как при расширении класса в PHP сделать все параметры необязательными?
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
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
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как сделать часы необязательными в средстве форматирования ISO_LOCAL_TIME
Anonymous » » в форуме JAVA - 0 Ответы
- 12 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Как сделать часы необязательными в средстве форматирования ISO_LOCAL_TIME
Anonymous » » в форуме JAVA - 0 Ответы
- 13 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Как сделать дочерние элементы программы чтения с экрана необязательными на Android
Anonymous » » в форуме Android - 0 Ответы
- 4 Просмотры
-
Последнее сообщение Anonymous
-