Reputation: 447
I just wrote code like this:
<?php
class test
{
// Force Extending class to define this method
abstract protected function getValue();
abstract protected function prefixValue($prefix);
// Common method
public function printOut() {
print $this->getValue() . "\n";
}
}
class testabs extends test{
public function getValue()
{
}
public function prefixValue($f)
{
}
}
$obj = new testabs();
?>
When I run this code, I received the error below:
Fatal error: Class test contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (test::getValue, test::prefixValue) in C:\wamp64\www\study\abstract.php on line 12
I understand the first part of this error. I changed the class test to abstract and the error is gone, but the or
part i can't understand.
Upvotes: 6
Views: 10900
Reputation: 8297
If you are going to add abstract methods, then you will need to make the class abstract
as well. That way, the class cannot be instantiated- only non-abstract sub-classes can be.
The method visibility (refer to the second sub-section Method Visiblilty) is not the same in the sub-class. Depending on whether you want the methods to be called by code outside of sub-classes, you can declare the (abstract) methods in class test
with visibility public
, or else declare the sub-class methods also with visibility protected
.
And note the second paragraph from the Class Abstraction page, which explains this:
When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private
<?php
abstract class test{
// Force Extending class to define this method
abstract protected function getValue();
abstract protected function prefixValue($prefix);
// Common method
public function printOut() {
print $this->getValue() . "\n";
}
}
class testabs extends test{
protected function getValue()
{
}
/**
* this method can be called from other methods with this class
* or sub-classes, but not called directly by code outside of this class
**/
protected function prefixValue($f)
{
}
}
$obj = new testabs();
// this method cannot be called here because its visibility is protected
$obj->prefixValues();// Fatal Error
?>
Upvotes: 9
Reputation: 1179
The key technical differences between an abstract class and an interface are:
Upvotes: 3
Reputation: 11975
Your class has abstract functions but is not declared as abstract, so you have two choices. Either declare the class as abstract
or provide an implementation of the abstract functions.
The first option (which you tried) allows the class to exist and be used by a concrete subclass that implements the functions. The second option means that the class is fully defined and can be used as is.
Upvotes: 2
Reputation: 5029
When your class
has abstract
methods it has to be declared abstract
too.
So the following is correct:
<?php
abstract class test
{
// Force Extending class to define this method
abstract protected function getValue();
abstract protected function prefixValue($prefix);
// Common method
public function printOut() {
print $this->getValue() . "\n";
}
}
Upvotes: 0