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 Overflow
🌐
PHP
php.net › manual › en › language.oop5.abstract.php
PHP: Class Abstraction - Manual
PHP has abstract classes, methods, and properties. Classes defined as abstract cannot be instantiated, and any class that contains at least one abstract method or property must also be abstract.
Discussions

Creating abstract properties in PHP - PHP - SitePoint Forums | Web Development & Design Community
Hi I was just wondering if it was possible to create/emulate abstract properties in PHP. This is for a Page class, which is extended for each page in the site. A skeleton in shown below: abstract class Page { pro… More on sitepoint.com
🌐 sitepoint.com
0
March 15, 2009
php - Is it good practice to declare and set properties in abstract classes? - Software Engineering Stack Exchange
I am using PHP and designing some abstract classes. I can declare properties with constant values within my abstract class and access/overwrite them from any class which extends this without re declaring them within any of the extending classes. More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
April 22, 2014
Base Class & Interfaces or Abstract Classes - I'm lost when to use what
Great question. There is so much confusion on the matter. First of all, technically here is no such thing in PHP as a "Base class". And Abstract class can be considered a good enough substitution, so you can think of Abstract class as a Base class. Hence we have only two matters left, Abstract class and Interface. Although they have overlapping responsibility, I devised a simple method to tell them apart: an Interface is sort of a public contract: it faces outwards. It tells other classes, what to expect from our class. Not to mention that a class could implement many Interfaces. an Abstract class is sort of a blueprint for other classes. It faces inwards. It tells other classes of the same type what they should be. And also implements the common functionality. Based on the above, this is how I do primitive class hierarchies I am able to design: decide the public contract my class has to offer, and create an Interface decide the inner workings and common functionality and create an Abstract class proceed to creating actual classes that extend the Base Abstract class (and already implements all its interfaces, though some Interfaces can be added to that particular class as well). So it could be like interface DeviceInterface { public function getDevice(); } abstract class AbstractDevice implements DeviceInterface { protected $device; public function getDevice() { return $this->device; } } class MobileDevice extends AbstractDevice { public function __construct() { $this->device = 'iPhone'; } } $device = new MobileDevice(); echo $device->getDevice(); More on reddit.com
🌐 r/PHPhelp
17
8
September 17, 2024
Abstract properties overwritten in sub class are undefined within parent constructor
There was an error while loading. Please reload this page · TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx) More on github.com
🌐 github.com
10
January 25, 2017
🌐
W3Schools
w3schools.com › php › php_oop_classes_abstract.asp
PHP OOP Abstract Classes
This means that the Audi, Volvo, and Citroen classes can use the public $name property as well as the public __construct() method from the Car class because of inheritance. But, intro() is an abstract method that should be defined in all the child classes and they should return a string. Let's look at another example where the abstract method has an argument: <?php abstract class ParentClass { // Abstract method with an argument abstract protected function prefixName($name); } class ChildClass extends ParentClass { public function prefixName($name) { if ($name == "John Doe") { $prefix = "Mr."; } elseif ($name == "Jane Doe") { $prefix = "Mrs."; } else { $prefix = ""; } return "{$prefix} {$name}"; } } $class = new ChildClass; echo $class->prefixName("John Doe"); echo "<br>"; echo $class->prefixName("Jane Doe"); ?> Try it Yourself »
🌐
SitePoint
sitepoint.com › php
Creating abstract properties in PHP - PHP - SitePoint Forums | Web Development & Design Community
March 15, 2009 - Hi I was just wondering if it was possible to create/emulate abstract properties in PHP. This is for a Page class, which is extended for each page in the site. A skeleton in shown below: abstract class Page { protected $title; abstract function displayContent(); protected function display(){ // display page, calling displayContent somewhere, and using the title from above // loads a page template } private function __destruct(){ $this->display()...
Top answer
1 of 4
2

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.

2 of 4
1

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.

🌐
Medium
medium.com › @Amir_M4A › in-the-world-of-php-programming-there-are-several-key-concepts-that-developers-need-to-understand-b2ed1916287f
PHP: Abstract Classes, Interfaces, and Traits Explained | Medium
September 23, 2024 - An abstract class in PHP is a class that cannot be instantiated and is meant to serve as a blueprint for other classes to inherit from. It contains abstract methods, which are declared but not implemented within the abstract class itself.
🌐
Zend
zend.com › blog › php-8-4-property-hooks
PHP 8.4 Property Hooks | New Features in PHP 8.4 | Zend
October 17, 2024 - And now we get to the juicy stuff: abstract classes. Prior to 8.4, an abstract class could define abstract methods. Starting in 8.4, an abstract class can ALSO define abstract properties, which MUST also define which hooks MUST be implemented:
Find elsewhere
🌐
Canned Atropine!!
igeek.info › 2014 › abstract-properties-in-php
Abstract properties in PHP | Canned Atropine!!
July 27, 2015 - As I was re-factoring data validation service in a project I’m working on (will open source it once first working draft is ready), I thought about making the $rules property abstract. But then I realized the absurdity of my thought and the fact that PHP does not have such a concept, not even like what C# has.
🌐
dailycomputerscience
dailycomputerscience.com › post › php-oop-fundamentals-abstract-classes-in-php
PHP OOP Fundamentals - Abstract Classes in PHP
October 11, 2024 - You want to share behavior (methods) and properties between related classes. You need to provide default functionality but allow subclasses to override certain methods. ... You want to define a contract that any class can implement, regardless of its inheritance hierarchy. You want to enforce method signatures without sharing any logic between classes. Abstract classes in PHP ...
🌐
Reddit
reddit.com › r/phphelp › base class & interfaces or abstract classes - i'm lost when to use what
r/PHPhelp on Reddit: Base Class & Interfaces or Abstract Classes - I'm lost when to use what
September 17, 2024 -

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 {
}
Top answer
1 of 5
7
Great question. There is so much confusion on the matter. First of all, technically here is no such thing in PHP as a "Base class". And Abstract class can be considered a good enough substitution, so you can think of Abstract class as a Base class. Hence we have only two matters left, Abstract class and Interface. Although they have overlapping responsibility, I devised a simple method to tell them apart: an Interface is sort of a public contract: it faces outwards. It tells other classes, what to expect from our class. Not to mention that a class could implement many Interfaces. an Abstract class is sort of a blueprint for other classes. It faces inwards. It tells other classes of the same type what they should be. And also implements the common functionality. Based on the above, this is how I do primitive class hierarchies I am able to design: decide the public contract my class has to offer, and create an Interface decide the inner workings and common functionality and create an Abstract class proceed to creating actual classes that extend the Base Abstract class (and already implements all its interfaces, though some Interfaces can be added to that particular class as well). So it could be like interface DeviceInterface { public function getDevice(); } abstract class AbstractDevice implements DeviceInterface { protected $device; public function getDevice() { return $this->device; } } class MobileDevice extends AbstractDevice { public function __construct() { $this->device = 'iPhone'; } } $device = new MobileDevice(); echo $device->getDevice();
2 of 5
5
Let's just get this out of the way first: While you technically can use inheritance (extending from another class) to share common behavior it is not typically recommended anymore to do so. That said of course if you're just going through a course the course might still go with pretty old recommendations. Instead the recommendation nowadays is to employ composition over inheritance . In practice this means that you'd be better off just implementing an interface and not even extending from anything at all in most cases. If anything base classes (abstract or not) only should (a "should" is a strong recommendation but not a hard rule) only implement methods that are the same for all subclasses. The guiding principle here is the LSP, the Liskov substitution principle (easily the most objectively quantifiable one out of SOLID ). The LSP basically says to make sure that an instance of any subclass can be used in place of the base class without problems. The most obvious violation of the LSP would be for example if you have a method in the base class and in a subclass you instead throw something like a NotImplementedException (something that can be seen in many real world projects sadly) because the subclass "doesn't use that method". It forces anyone using that base class to know about the implementation details of its subclasses which is something that would go against the goal of encapsulation. Overall these recommendations and principles make abstract classes or base classes in general a very rare breed. Well, not rare as in that you won't see it in real-world code but rare as in it probably shouldn't have been done this way. Strictly speaking base classes, abstract classes, inheritance itself is not actually necessary at all. For example instead of an abstract class Bar that wants its subclasses to implement an abstract method foo() which would then be used in a non-abstract method bar() could also be modelled as such: interface FooInterface { public function foo(); } class FooImplementation implements FooInterface { public function foo() { // ... } } class Bar { private $foo; public function __construct(FooInterface $foo) { $this->foo = $foo; } public function bar() { $this->foo->foo(); // ... } }
🌐
Medium
medium.com › @andreibirta95 › understanding-and-utilizing-abstract-classes-in-php-7eeeeeabc444
Understanding and Utilizing Abstract Classes in PHP | by Andrei Birta | Medium
January 27, 2023 - In PHP, an abstract class is a class that cannot be instantiated on its own, but can be extended by other classes. An abstract class is used as a blueprint or template for other classes and it contains methods that must be implemented by its ...
🌐
Exakat
exakat.io › home › reviewing property declaration in php
Reviewing Property Declaration In PHP - Exakat
December 25, 2023 - This behavior looks like an abstractproperty : like for a method, the abstract property is defined in the parent class, yet MUST be defined in the child class to acquire a local default value. For methods, this is enforced by the PHP engine, while there is no such behavior for properties, nor for constants.
🌐
Alex Web Develop
alexwebdevelop.com › home › php abstract classes explained
PHP Abstract Classes Explained - Alex Web Develop
September 2, 2022 - When you create an object of that class, PHP follows the instructions and creates the object. For example, you can write a “Computer” class with attributes such as CPU type, memory and power consumption, and with methods such as turn on, turn off and stand-by. This class provides everything that is needed to create a Computer object, because all the attributes and methods are clearly defined. An abstract class, on the other hand, is like a requirements sheet.
🌐
Phpenthusiast
phpenthusiast.com › learn, practice, and apply object-oriented php › object-oriented php tutorials › abstract classes and methods in php | phpenthusiast
Abstract classes and methods in PHP | PHPenthusiast
February 17, 2017 - Let's add to our example the protected property, $tankVolume, and public method with the name of setTankVolume(). abstract class Car { // Abstract classes can have properties protected $tankVolume; // Abstract classes can have non abstract methods public function setTankVolume($volume) { $this -> tankVolume = $volume; } // Abstract method abstract public function calcNumMilesOnFullTank(); }
🌐
W3Schools
w3schools.in › php › abstract-classes
PHP Abstract Classes
Abstract properties are like abstract methods, but do not have a body and are declared using the var keyword.
🌐
FlatCoding
flatcoding.com › home › abstract class in php: how it works & examples
Abstract Class in PHP: How It Works & Examples - FlatCoding
April 15, 2025 - Abstract classes vs. interfaces—abstract classes allow properties and mixed methods, while interfaces enforce method signatures. An abstract class in PHP acts as a template for other classes. You cannot create an object from it directly.
🌐
GitHub
github.com › microsoft › TypeScript › issues › 15247
Abstract properties overwritten in sub class are undefined within parent constructor · Issue #15247 · microsoft/TypeScript
January 25, 2017 - Abstract properties overwritten in sub class are undefined within parent constructor#15247 · Copy link · Labels · DuplicateAn existing issue was already createdAn existing issue was already created · PMXScott · opened · on Apr 18, 2017 · Issue body actions ·
Published   Apr 18, 2017
🌐
YouTube
youtube.com › bitfumes
OOP PHP | Traits: Abstract Functions and Properties #22 - YouTube
Object Oriented Programming ( OOP ) in PHP.Learn about Abstract functions and properties of Traits==Support ==Become My Patron here https://goo.gl/NcvDQhYou ...
Published   August 21, 2017
Views   8K