I'm a relative newbie to OOP, and I am getting this error on a learning exercise.
Class contains 6 abstract methods and must therefore be declared abstract or implement the remaining methods
the error is being thrown from a child class of an abstract class, implementing an interface. I understand that children of an abstract class must implement all abstract methods, but I am not declaring any abstract methods in the parent class or the interface. Shouldn't I only be getting this error if I am not including, in the child class, a declared abstract method from the abstract class or interface?
child class:
class OuterViewDecorator extends AbstractViewDecorator
{
const DEFAULT_TEMPLATE = "/var/www/portfolio/simple-php/templates/layout.php";
public function render() {
$data["innerview"] = $this->view->render();
return $this->renderTemplate($data);
}
}
parent class:
abstract class AbstractViewDecorator implements ViewInterface
{
const DEFAULT_TEMPLATE = "default.php";
protected $template = self::DEFAULT_TEMPLATE;
protected $view;
public function __construct(ViewInterface $view)
{
$this->view = $view;
}
public function render()
{
return $this->view->render();
}
public function renderTemplate(array $data = array())
{
extract($data);
ob_start();
$template = include $this->template;
return ob_get_clean($template);
}
}
interface:
interface ViewInterface
{
public function setTemplate($template);
public function getTemplate();
public function __set($field, $value);
public function __get($field);
public function __isset($field);
public function __unset($field);
public function render();
}
thanks for any help
You are saying it is implementing an interface.
Between all of the inheriting classes must implement all of the interface methods
So for example your AbstractViewDecorator could implement 2 of the methods, and OuterViewDecorator could implement the last 4, or OuterViewDecorator could do all 6.. As long as all of the methods are implements in the class inheritance chain.
Related
So you can't make an abstract static function in php.
The alternatives as I see them are to:
Make the function non-static and write extra boilerplate code to create and store the object so I can access that function.
abstract class Foo {
abstract public function bar();
}
abstract class Good {
public function bar() {
...
}
}
// boilerplate to access Good->bar()... potentially a lot in multiple files
$g = new Good();
$g->bar();
Fill in the static function in my abstract class with a BadMethodCallException, so that any call to a child class which doesn't implement it will throw the exception.
abstract class Foo {
public static function bar() {
throw new BadMethodCallException("Not Implemented By Child Class :(");
}
}
class Good extends Foo {
public static function bar() {
// ...
}
}
class Bad extends Foo {
// no bar implementation
}
Good::bar(); // works
Bad::bar(): // exception
I'm leaning towards 2. but was wondering if there's any community consensus on this issue or best practices.
I ended up making an interface with a static function, then implementing the interface in the abstract class. This forces the child classes to define the method, which is basically what I wanted with an abstract static function.
interface ModelFactoryInterface {
public static function offer();
}
abstract class ModelHelper implements ModelFactoryInterface {
protected $tester;
public function __construct($tester) {
$this->tester = $tester;
}
}
/* Location
* ------------------------------------------------------ */
final class LocationHelper extends ModelHelper {
public static function offer() {
return new Location(...)
}
}
I have an abstract class that declares the methods required to its children. It also has a construstor that its children inherits. How can I make the abstract class affect the children of the classes that extends it. To further clarify my question, here is my case:
The Abstract Class (abstract.php):
<?php
include_once 'database.php';
include_once 'validation.php';
abstract class DataOperations extends DatabaseConnection {
//The error string shared by all children of DataOperations
//This will be the message to be displayed in case validation failure
public $validator;
public $err_valid_string;
/**
* The DataOperations' constructor ensures that all of its children can perform database operation
* by automatically starting it for them. In case a child overrides this constructor, this child
* must explicitly start the connection to prevent fatal errors. Also, $validator must be re-instantiated
*/
public function __construct() {
$this->startDBConnection();
$this->validator = new InputValidator();
}
public function __destruct() {
}
abstract public function validateData();
abstract public function loadRecord($key, $cascade);
abstract public function saveRecord();
abstract public function updateRecord();
abstract public function deleteRecord();
}
?>
Now, here is the child object that extends the DataOperations abstract class
class Guest extends DataOperations {
//some properties here
public function validateData() {
//implementation
}
public function newRecord(implementation) {
//implementation
}
public function loadRecord($key, $cascade){
//implementation
}
public function saveRecord() {
//implementation
}
public function updateRecord() {
//implementation
}
public function deleteRecord() {
//implementation
}
}
?>
And here is another class, which is a child of Guest
class Booking extends Guest {
//some properties here
public function validateData() {
//implementation
}
public function newRecord(implementation) {
//implementation
}
public function loadRecord($key, $cascade){
//implementation
}
public function saveRecord() {
//implementation
}
public function updateRecord() {
//implementation
}
public function deleteRecord() {
//implementation
}
}
?>
The problem is, if I remove a method in Booking, say deleteRecord(), PHP won't throw an error because I think abstract class doesn't affect its 'grandchildren'. How can I fix this? I thought of using interfaces but my system already has 11 classes that depends to some methods of the abstract class. It will require intensive refactoring.
As you himself stated interface is best suited solution. Like
include_once 'database.php';
include_once 'validation.php';
interface DbInterface {
abstract public function validateData();
abstract public function loadRecord($key, $cascade);
abstract public function saveRecord();
abstract public function updateRecord();
abstract public function deleteRecord();
}
class DataOperations extends DatabaseConnection {
//The error string shared by all children of DataOperations
//This will be the message to be displayed in case validation failure
public $validator;
public $err_valid_string;
/**
* The DataOperations' constructor ensures that all of its children can perform database operation
* by automatically starting it for them. In case a child overrides this constructor, this child
* must explicitly start the connection to prevent fatal errors. Also, $validator must be re-instantiated
*/
public function __construct() {
$this->startDBConnection();
$this->validator = new InputValidator();
}
public function __destruct() {
}
}
class Guest extends DataOperations implements DbInterface {
- - -
}
class Booking extends Guest implements DbInterface {
- - -
}
First as you see I removed abstract from parent class as I assuming only those methods are abstract. Second as per your problem of 11 classes depend on Abstract class, I would say As you only remove abstract methods, Class implementing abstract methods now should implement interface. It is one time needed task. While classes using other normal methods of abstract class work like previous.
The best and cleanest way would be to have your "BOOKING" class extend the "DATAOPERATIONS" class, instead of GUEST, because looks like you don't have any extra methods in the BOOKING class. other wise make and interface and implement it. That is not the preferred way but you would have to give more info your situation.
To be clear, re-declaring a method in a child class will overwrite the parent class's implementation of that method when called from the child class, while not affecting any additional functionality provided by extending the parent class:
class a
{
function hello()
{
echo "Hello";
}
function goodbye()
{
echo "Goodbye";
}
}
/**
* class b overwrites class a's implementation of method goodbye but will retain
* it's definition for method hello
*/
class b extends a
{
function goodbye()
{
echo "See ya!";
}
}
$object = new b();
$object->hello(); // Hello
$object->goodbye();// See ya!
It appears that you want to implement a consistent interface across multiple class definitions. If this is the case, you will likely want to explore using PHP's interfaces.
These allow you to specify the methods that must exist in your class definition along with their set of arguments (collectively known as the signature). Your class definitions will implement an interface and if your definition does not meet the interface implementation specification, a fatal error will be thrown.
From the PHP manual:
// Declare the interface 'iTemplate'
interface iTemplate
{
public function setVariable($name, $var);
public function getHtml($template);
}
// Implement the interface
// This will work
class Template implements iTemplate
{
private $vars = array();
public function setVariable($name, $var)
{
$this->vars[$name] = $var;
}
public function getHtml($template)
{
foreach($this->vars as $name => $value) {
$template = str_replace('{' . $name . '}', $value, $template);
}
return $template;
}
// This will not work
// Fatal error: Class BadTemplate contains 1 abstract methods
// and must therefore be declared abstract (iTemplate::getHtml)
class BadTemplate implements iTemplate
{
private $vars = array();
public function setVariable($name, $var)
{
$this->vars[$name] = $var;
}
}
You can find more information about interface in the PHP manual:
http://us2.php.net/interface
Finally, it looks like you are hoping to define a common constructor for the child classes. Your child classes can both extend the DataOperations class while implementing a separate interface:
class Guest extends DataOperations implements DatabaseWriter
...
base class:
abstract class Challenge
{
abstract **static** public function getName();
}
now two classes from it:
class ChallengeType1 extends Challenge
{
public **static** function getName()
{
return 'Swimming';
}
}
class ChallengeType2 extends Challenge
{
public **static** function getName()
{
return 'Climbing';
}
}
as you all might know, we can't use static, but it would be reasonable. So I can't do like that: for example, I have the classname, so I want to know it's name: ChallengeType2::getName(); - it will fail! First I should construct the object - looks unnecessary (not to mention, what it this class have very complex initialization?)
Turns out you cannot have static abstract methods on an abstract class. See here:
Why does PHP 5.2+ disallow abstract static class methods?
But you can declare an interface to require a static method.
Here is an example that compiles:
Working Example
<?php
interface IChallenge
{
static function getName();
}
abstract class Challenge implements IChallenge
{
}
class ChallengeType1 extends Challenge
{
public static function getName()
{
return 'Swimming';
}
}
class ChallengeType2 extends Challenge
{
public static function getName()
{
return 'Climbing';
}
}
If I have an abstract class like this:
abstract class MyApp
{
public function init()
{
$this->stuff = $this->getStuff();
}
public function getStuff()
{
return new BlueStuff();
}
}
And then I have a class that extends from this abstract class like this:
class MyExtendedClass extends MyApp
{
public function init()
{
parent::init();
}
public function getStuff()
{
return new RedStuff();
}
}
If I do:
$myobj = new MyExtendedClass();
$myobj->init();
Why does the method getStuff from the child class get called? Isn't $this in the context of the abstract class? If so, shouldn't the method of the abstract class get called?
Thanks!
New answer
In PHP you can use subclasses as if all methods in the parent class that don't exist in the subclass have been copied to the subclass. So your example would be the same as:
class MyExtendedClass extends MyApp {
public function init() {
$this->stuff = $this->getStuff();
}
public function getStuff() {
return new RedStuff();
}
}
Just think of the subclass as having all code of the parent class and you're normally all right. There is one exception to this rule: properties. A private property of a class can only be accessed by that class, subclasses can't access the private properties of parent classes. In order to do that you need to change the private property into a protected property.
Original answer
Abstract classes in PHP are just like regular classes with one big difference: they can't get initiated. This means you can't do new AbstractClass();.
Since they work exactly like regular classes for everything else, this also is the case for extending classes. This means that PHP first tries to find the method in the initiated class, and only looks in the abstract classes if it doesn't exist.
So in your example this would mean that the getStuff() method from MyExtendedClass is called. Furthermore, this means you can leave out the init() method in MyExtendedClass.
I have created a class here that contains an abstract method , it always return this errors to me even though that method is declared abstract. Here's my whole code
<?php
interface Validator{
public function isValid($input);
public function getErrors();
}
class Validator_AbstractValidator
{
protected $_errors = array();
abstract public function isValid($input);
public function getErrors()
{
return $this->_errors;
}
protected function _addError($message){
$this_errors[] = $message;
}
}
class Validator_MinimumLength extends Validator_AbstractValidator
{
protected $_minLength;
public function __construct($minLength)
{
$this_minLength = $minLength;
}
public function isValid($input){
if (strlen($input) > $this->_minLength) {
return true;
} else {
$this->_addError("Input must be at least {$this_minLength}");
return false;
}
}
}
class Validator_NoSpaces extends Validator_AbstractValidator{
public function isValid($input) {
if (preg_match('/\s/', $input)){
$this->_addError("Spaces are not allowed");
return false;
}
return true;
}
}
interface Form_ElementInterface extends Validator{ }
class Form_Element_AbstractElement extends Validator_AbstractValidator implements Form_ElementInterface
{
protected $_validators = array();
public function addValidator(Validator $validator)
{
$this->_validators[] = $validator;
}
public function getValidators()
{
return $this->_validators;
}
protected function _addErrors(array $errors)
{
foreach ($errors as $error) {
$this->_addError($error);
}
}
public function hasErrors()
{
return (count($this->getErrors()) !== 0);
}
public function isValid($input)
{
foreach ($this->_validators as $validator) {
if (!$validator->isValid($input)) {
$this->_addErrors($validator->getErrors());
}
}
return !$this->hadErrors();
}
}
class Form_Element extends Form_Element_AbstractElement
{
public function __construct($value,$minLength = 8)
{
$this->addValidator(new Validator_NoSpaces($value));
$this->addValidator(new Validator_MinimumLength($minLength));
//...
}
}
The error is this Fatal error: Class Validator_AbstractValidator contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Validator_AbstractValidator::isValid) in C:\xampp\htdocs\beatbeast\includes\Db\Validator.php on line 21
but line 21 only contains this
}
Is there anything I've missed?
Even though I added the keyward abstract in Class Validator_AbstractValidator class I encountered this problem
Fatal error: Can't inherit abstract function Validator::isValid() (previously declared abstract in Validator_AbstractValidator) in C:\xampp\htdocs\beatbeast\includes\Db\Validator.php on line 57
but once again line 57 only contains this
{
PHP 5 introduces abstract classes and methods. It is not allowed to
create an instance of a class that has been defined as abstract. 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.
if a class is not declared as abstract, it cannot contain any abstract methods.
abstract class Validator_AbstractValidator
{
//...
}
Update:
class Form_Element_AbstractElement extends Validator_AbstractValidator implements Form_ElementInterface
You are extending an abstract class (Validator_AbstractValidator) and also implementing the interface Validator all together, that is not possible.
Extend the class and implement the interface individually and implement the interface Validator in a separate class, not in Validator_AbstractValidator because it's an abstract class you've already declared before.
Implementing of interface Validator:
class from_validator_interface implement Validator{
// you have to implement all the methods declared in the interface, in your case
// `isValid` and `getErrors` both methods have to be implemented
}
Extending of abstract class Validator_AbstractValidator:
class someClass extends Validator_AbstractValidator{
// you don't need to implement all the abstract methods like interface
}
Some usefull links: php.net, other and more.
Your code works fine for me once I add abstract to Validator_AbstractValidator, which you say you already did.
abstract class Validator_AbstractValidator
{ ...
I had the same error with the following code :
Interface Fruit
{
public function getCalories() ;
public function countSeeds() ;
}
Interface Vegetable
{
public function getVitamins() ;
public function getCalories() ;
}
class Tomato implements Fruit, Vegetable
{
...
}
Here was my solution, I added a third parent interface.
Interface Food
{
public function getCalories() ;
}
Interface Fruit extends Food
{
public function countSeeds() ;
}
Interface Vegetable extends Food
{
public function getVitamins() ;
}
class Tomato implements Fruit, Vegetable
{
...
}