Please help me. I need a better understanding PHP OOP principles.
If I have a class property which is immutable for all of the class instances it should be defined as static? If so, is there a way to be sure that static properties are defined in all classes of that type? As I read in PHP manual, static properties cannot be controller neither by the interface nor by abstract classes? Or am I wrong?
Simple example.
<?php
// Parent class
abstract class Employee
{
abstract public function getAlias();
}
// Child classes
class Manager extends Employee
{
public function getAlias()
{
return 'manager';
}
}
class Security extends Employee
{
public function getAlias()
{
return 'security';
}
}
Tell me, where an alias
property should be placed?
I have to be sure that any Employee descendants that will be created in future will have that property defined. Is it OK to keep that kind of properties in dynamic methods? Or they should be placed in constants, static methods or static properties?
alias
property (as far as any similar one) can be stored in DB table and used for Single Table Inheritance - instantiate a proper class based on retrieved DB records' alias. – Roman Oct 21 '17 at 18:09Manager::class
would give you the fully qualified class name. Also, shouldn't your ORM handle that for you? Don't get me wrong. It's a valid question. It just smells like something that could or should be handled through type checking instead. But to decide that needs some context. – Gordon Oct 21 '17 at 18:17Employee
instance should either contain only the business logic or only contain the SQL code. And a table name is something that should actually be governed via configuration instead of hardcoding it in the class definition. – tereško Oct 24 '17 at 21:43