You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an abstract class like this (a bit simplified to remove all the fluff)
<?phpdeclare(strict_types = 1);
namespaceSimbiat\Abstracts;
abstractclass Page
{
#Current breadcrumb for navigationprotectedarray$breadCrumb = [];
#Sub service nameprotectedstring$subServiceName = '';
#Time of last data modification (defaults to current time on initialization)protectedint$lastModified = 0;
publicfinalfunction__construct()
{
#Check that subclass has set appropriate propertiesforeach (['subServiceName', 'breadCrumb'] as$property) {
if(empty($this->{$property})) {
thrownew \LogicException(get_class($this) . ' must have a non-empty `'.$property.'` property.');
}
}
#Set last modified data$this->lastModified = time();
}
#Generation of the page dataabstractprotectedfunctiongenerate(array$path): array;
I then have a child class like this:
<?phpdeclare(strict_types = 1);
namespaceSimbiat\usercontrol\Pages;
/** * Page to unsubscribe email addresses from messages */class Unsubscribe extends Page
{
#Current breadcrumb for navigationprotectedarray$breadCrumb = [
['href' => '/uc/unsubscribe', 'name' => 'Unsubscribe']
];
#Sub service nameprotectedstring$subServiceName = 'unsubscribe';
/** * Actual page generation based on further details of the $path * @param array $path URL path * * @return array */protectedfunctiongenerate(array$path): array
{
#Doing stuff, not relevant to the main question$outputArray = [];
return$outputArray;
}
}
In child class fields $breadCrumb and $subServiceName are highlighted with Class overrides a field of a parent class warning. Based on documentation for the warning I have a few options:
Rename property. Can't do that, that because they are used in a bunch of functions in the abstract class
Move property initialization into the constructor. Can't do that, since my constructor is final, and I can't override it. Setting up the fields in the final constructor is also not an option, because then I would need to carry the values of respective fields as arguments anywhere the child class is initialized. And my goal is to have those expected values in the respective child class itself. Kind of like customization of defaults, I guess.
use @property annotation to re-define type-hints. Not sure if this makes sense even, since I only need to override the value, but I tried adding @property array $breadCrumb to child class PHPDoc (under class description), and it did not do anything.
Am I doing something wrong or am I missing something? Is there a better way? Abstract class is meant to define the structure and basic functionality of the page entities, and then each child class is supposed to implement all of them without changes, but override default values of some attributes (when needed) and implement a custom generation logic. Is there a better way to do that?
The text was updated successfully, but these errors were encountered:
Question
I have an abstract class like this (a bit simplified to remove all the fluff)
I then have a child class like this:
In child class fields
$breadCrumb
and$subServiceName
are highlighted withClass overrides a field of a parent class
warning. Based on documentation for the warning I have a few options:Rename property
. Can't do that, that because they are used in a bunch of functions in the abstract classMove property initialization into the constructor
. Can't do that, since my constructor is final, and I can't override it. Setting up the fields in the final constructor is also not an option, because then I would need to carry the values of respective fields as arguments anywhere the child class is initialized. And my goal is to have those expected values in the respective child class itself. Kind of like customization of defaults, I guess.use @property annotation to re-define type-hints
. Not sure if this makes sense even, since I only need to override the value, but I tried adding@property array $breadCrumb
to child class PHPDoc (under class description), and it did not do anything.Am I doing something wrong or am I missing something? Is there a better way? Abstract class is meant to define the structure and basic functionality of the page entities, and then each child class is supposed to implement all of them without changes, but override default values of some attributes (when needed) and implement a custom generation logic. Is there a better way to do that?
The text was updated successfully, but these errors were encountered: