There is no such thing as defining a property.
You can only declare properties because they are containers of data reserved in memory on initialization.
A function on the other hand can be declared (types, name, parameters) without being defined (function body missing) and thus, can be made abstract.
"Abstract" only indicates that something was declared but not defined and therefore before using it, you need to define it or it becomes useless.
Answer from Mathieu Dumoulin on Stack OverflowThere is no such thing as defining a property.
You can only declare properties because they are containers of data reserved in memory on initialization.
A function on the other hand can be declared (types, name, parameters) without being defined (function body missing) and thus, can be made abstract.
"Abstract" only indicates that something was declared but not defined and therefore before using it, you need to define it or it becomes useless.
No, there is no way to enforce that with the compiler, you'd have to use run-time checks (say, in the constructor) for the $tablename variable, e.g.:
class Foo_Abstract {
public final function __construct(/*whatever*/) {
if(!isset($this->tablename))
throw new LogicException(get_class($this) . ' must have a $tablename');
}
}
To enforce this for all derived classes of Foo_Abstract you would have to make Foo_Abstract's constructor final, preventing overriding.
You could declare an abstract getter instead:
abstract class Foo_Abstract {
abstract public function get_tablename();
}
class Foo extends Foo_Abstract {
protected $tablename = 'tablename';
public function get_tablename() {
return $this->tablename;
}
}
Creating abstract properties in PHP - PHP - SitePoint Forums | Web Development & Design Community
php - Is it good practice to declare and set properties in abstract classes? - Software Engineering Stack Exchange
Base Class & Interfaces or Abstract Classes - I'm lost when to use what
Abstract properties overwritten in sub class are undefined within parent constructor
Videos
This may be a personal preference, but I avoid having base classes for the sake of sharing properties. Particularly in data classes. I don't mind the repetition, and I avoid locking my classes into a fixed "data" hierarchy.
There are enough problems with inheritance, particularly over time as the code base grows, that I am reluctant to share methods through a class hierarchy, and even more reluctant to share properties through a class hierarchy.
...
Hm. I just noticed that you mentioned "static" properties. Rather than static (i.e. global) properties in a base class, you're better off with a separate, single instance of something dispensed by a factory. Make sure you provide for concurrency to avoid race conditions.
To answer your question, yes. If the abstract class is the logical place for the property then put it there. Use the tools that classes and OOP gives you.
"overtime the original structure/layout of these abstract properties is forgotten and could be left at there default static values or even forgotten(not used) completely."
This implies to me that developers using/creating the abstract class or child classes are required to do something with these properties. If that is true, try to come up with a class design that forces a developer to deal with this. That's better then to trust everyone to always do it correctly.
You mention changing static values. Static properties are shared between all class instances, so by changing their value in one object you change it for all. Multiple objects changing global state is something you want to avoid, because it is difficult to keep all changes in the correct order.
I understand a base class provides common methods to other classes that extend it.
And from what I've read, Interfaces provide a way to make sure that a method exists in your class.
It seems that Abstract class combines the two into a single file, so I'm a bit confused on when to use what.
Let's say I have a method called get_device() that is shared amount LaptopDevice, DesktopDevice, and MobileDevice classes.
Each device class gets a list of devices from its device type.
Would I do something like:
abstract class AbstractDevice {
public function get_device() {
return $this->device;
}
}
class MobileDevice extends AbstractDevice {
}or this
class Device implements DeviceInterface {
public function get_device() {
return $this->device;
}
}
class MobileDevice extends Device implements DeviceInterface {
}