Is is possible to access a variable in a PHP abstract class, i.e.
abstract class Settings
{
public $application = array
(
'Name' => 'MyApp',
'Version' => '1.0.0',
'Date' => 'June 1, 2017'
)
}
echo Settings::application ['Name']; // doesn't work
You could make the variable static, as long as it doesn't need to allow differentiation across instances (i.e. instance variable):
<?php
// example code
abstract class Settings
{
public static $application = array
(
'Name' => 'MyApp',
'Version' => '1.0.0',
'Date' => 'June 1, 2017'
);
}
echo Settings::$application ['Name'];
Run it in this playground example.
Though your original access of application
was similar to that of a constant. use const
to declare a Class constant:
abstract class Settings
{
const application = array
(
'Name' => 'MyApp',
'Version' => '1.0.0',
'Date' => 'June 1, 2017'
);
}
echo Settings::application ['Name'];
Run it in this playground example.
Abstract Classes can not be directly instantiated as they rely on child classes to fully implement the functionality.
So if you want to check your variable I would make new class and inherit from your Settings class. You will have to use it with inheritance anyway.
class MySettings extends Settings
{
....
}
$mySettings = new MySettings();
echo $mySettings->application['Name'];
More about abstract classes http://culttt.com/2014/03/26/abstract-classes/