Answer the question
In order to leave comments, you need to log in
How to correctly implement the Parent->Child classes?
Hello.
Not so long ago I began to study OOP, so I'll forgive you not to judge strictly :)
We need to solve such a problem. There is a class:
<?php declare(strict_types=1);
class SubscribeController
{
public function notify(Request $request)
{
$email = $request->json('from.email');
if (!isset($email)) {
return self::userError('from.email is missing.');
}
if (!is_string($email)) {
return self::userError('from.email must be a string.');
}
$name = $request->json('from.name');
if (isset($name) && !is_string($name)) {
return self::userError('from.name must be either null or a string.');
}
try {
$mailer = Factory::createMailer(app()->basePath(), new EmailAddress($email, $name));
$mailer->notify('тут название события', new EmailAddress('тут почта', 'тут имя'));
} catch (Exception $e) {
return self::error($e->getMessage());
}
return self::ok();
}
privat static function userError($message)
{
return self::error($message, 400);
}
privat static function error($message, $code = 500)
{
return response()->json($message, $code);
}
privat static function ok()
{
return response()->json("ok");
}
}
class Didgest extends SubscribeController
{
public function notify(Request $request)
{
$email = $request->json('from.email');
if (!isset($email)) {
return self::userError('from.email is missing.');
}
if (!is_string($email)) {
return self::userError('from.email must be a string.');
}
$name = $request->json('from.name');
if (isset($name) && !is_string($name)) {
return self::userError('from.name must be either null or a string.');
}
try {
$mailer = Factory::createMailer(app()->basePath(), new EmailAddress($email, $name));
$mailer->notify('didgest', new EmailAddress('email', 'name'));
} catch (Exception $e) {
return self::error($e->getMessage());
}
return self::ok();
}
}
$mailer->notify('--->NEW<---', new EmailAddress('email', 'name'));
$mailer->notify('--->DIDGEST<---, new EmailAddress('email', 'name'));
$email = $request->json('from.email');
if (!isset($email)) {
return self::userError('from.email is missing.');
}
if (!is_string($email)) {
return self::userError('from.email must be a string.');
}
$name = $request->json('from.name');
if (isset($name) && !is_string($name)) {
return self::userError('from.name must be either null or a string.');
}
$email, $name
, что вполне логично, а как их сделать видимыми из класса-родителя в классах-потомках ума не приложу :( или может вы подскажите, какой-то более элегантный способ в такой ситуации. Answer the question
In order to leave comments, you need to log in
Паттерн диспетчер событий.
https://github.com/ElisDN/yii2-demo-shop/blob/mast...
Там где нужно вызвать https://github.com/ElisDN/yii2-demo-shop/blob/mast...
Обработчик https://github.com/ElisDN/yii2-demo-shop/blob/mast...
Зависимости через DI
https://github.com/ElisDN/yii2-demo-shop/blob/mast...
Вот как я понимал события
<?php
$events = [];
$on = function (string $name, callable $func) use (&$events)
{
$events[ $name ][] = $func;
};
$fire = function (string $name, $emitter, ...$arguments) use (&$events)
{
if (! isset($events[ $name ])) return;
foreach ($events[ $name ] as $func) {
call_user_func($func, $name, $emitter, ...$arguments);
}
};
// когда машина выйдет с завода - поехали!
$on('car-created', function ($event, $car, ...$comments) { var_dump($car, $comments); });
// ...some application code
// пришло время, тачка готова.
$fire('car-created', $car, 'faster', 'darling');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question