Daniela, there's no reason to use this as an abstract class, it's not its purpose.
As stated in documentation
Classes defined as abstract cannot be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature - they cannot define the implementation.
Instead, use a common class
<?php
class FormatHelper
{
public static function formatArrayIdValue($result, $id)
{
$array = [];
foreach ($result as $val) {
extract($val);
$array[$id] = urldecode($val);
}
return $array;
}
}
and make sure you include it in your script:
<?php
require_once('FormatHelper.php');
print_r(
FormatHelper::formatArrayIdValue(['foo bar baz'], 'foo')
);
// output:
// Array ( [foo] => foo bar baz )
And the proper usage of abstract classes is:
<?php
abstract class AbstractFormatHelper
{
// note that abstract method has no body
abstract public static function requiredMethodToBeImplementedInChildClass();
}
class FormatHelper extends AbstractFormatHelper
{
public static function requiredMethodToBeImplementedInChildClass()
{
return 'Implemented!';
}
public static function formatArrayIdValue($result, $id)
{
$array = [];
foreach ($result as $val) {
extract($val);
$array[$id] = urldecode($val);
}
return $array;
}
}
Conclusions & suggestions
Operators
- Use
::
operator for accessing static
methods.
MyClass::myStaticMethod();
- Use
->
operator for accessing methods of object.
$obj = new MyClass();
$obj->myNonStaticMethod();
Inheritance with abstract
As a Rule of thumb (ROT) abstract class is defined as abstract
and contains at least one abstract
method. That means that the main purpose of abstract classes is allowing its inheritance,
Although as Jeto pointed as a side effect if the class is defined as abstract
, even if it doesn't contain any non_static methods it cannot be instantiated, however, this is not purpose for using abstract classes. Instead, you should use abstract classes only in case when you want to force developers (maybe yourself) for creating methods in their classes which extends your abstract class.
analyse this sample:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
class MyClass
{
public static function myStaticMethod()
{
return ' this is myStaticMethod()';
}
public function myNonStaticMethod()
{
return 'this is myNonStaticMethod()';
}
}
abstract class AbstractFormatHelper
{
abstract public static function requiredMethodToBeImplementedInChildClass();
abstract protected static function otherProtectedAbstractMethod();
// final abstract protected static function otherProtectedFinalAbstractMethod(); // you even can't declare it
public static function foo()
{
return "foo() doesn't need to be implemented";
}
protected static function bar()
{
return "this doesn't need to be implemented but will be not accessible if you won't change it to public";
}
final protected static function baz()
{
return "baz() is final PROTECTED, you can NOT override it in the child, but you still can access it in child by getter ";
}
final private static function boo()
{
return 'boo() is final PRIVATE but and you can NOT access it in child by getter ';
}
}
class FormatHelper extends AbstractFormatHelper
{
public static function requiredMethodToBeImplementedInChildClass()
{
return 'Implemented!';
}
public static function otherProtectedAbstractMethod()
{
return 'erProtectedAbstractMethod() implemented';
}
public static function foo()
{
return parent::foo() . " but foo() can be implemented";
}
public static function bar()
{
return 'method bar() which was protected now can be public OR protected NOT private (see below)';
}
// That wouldn't work with private
// private static function bar()
// {
// return parent::bar() . ' I wanted to override it in child as a private but it is impossible';
// }
public static function getBaz()
{
return self::baz(); // you can still access it in child class, cause is protected
}
// public static function getBoo()
// {
// return self::boo(); // it won't work ass parent boo is private
// }
}
echo '<pre> OPERATORS' . PHP_EOL . PHP_EOL;
// operands
echo MyClass::myStaticMethod() . PHP_EOL;
echo MyClass::myNonStaticMethod() . PHP_EOL; // Warning In PHP 7, calling non-static methods statically is deprecated
$obj = new MyClass();
echo $obj->myStaticMethod() . PHP_EOL; // wouldn't use that
echo $obj::myStaticMethod() . PHP_EOL;
echo $obj->myNonStaticMethod() . PHP_EOL;
echo $obj::myNonStaticMethod() . PHP_EOL; // Warning In PHP 7, calling non-static methods statically is deprecated
echo PHP_EOL . PHP_EOL;
echo 'INHERITANCE WITH `abstract`' . PHP_EOL . PHP_EOL;
echo FormatHelper::foo() . PHP_EOL;
echo FormatHelper::bar() . PHP_EOL;
// echo FormatHelper::baz() . PHP_EOL; // you cannot access it as it's protected and final
echo FormatHelper::getBaz() . PHP_EOL;
Namespaces
Although this tip exceeds boundaries of this answer consider using namespaces in the future, especially, when your project will grow, so with proper autoload you will be able to use classes without requiring them each time like.
<?php
require_once('autoloader.php');
print_r(\Your\Namespace\FormatHelper::formatArrayIdValue(['foo bar baz'], 'foo');
print_r(\Other\Namespace\OtherHelper::format('foo');
print_r(\Quite\Other\Something\SomeHelper::someMethod());
Conventions
note, for the convention I renamed the classes and methods names, also added $id
param as it was missing.