Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare abstract method in non-abstract class in PHP?

class absclass {
    abstract public function fuc();
}

reports:

PHP Fatal error: Class absclass contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (absclass::fuc)

I want to know what it means by implement the remaining methods,how?

like image 789
user198729 Avatar asked Mar 03 '10 13:03

user198729


People also ask

Can you declare an abstract method in a non abstract class?

Abstract method declarations are only permitted in abstract classes. public abstract void MyMethod(); The implementation is provided by a method override, which is a member of a non-abstract class. It is an error to use the static or virtual modifiers in an abstract method declaration.

Can't have an abstract method in a non abstract class the class?

A normal class(non-abstract class) cannot have abstract methods. In this guide we will learn what is a abstract class, why we use it and what are the rules that we must remember while working with it in Java. An abstract class can not be instantiated, which means you are not allowed to create an object of it.

Can abstract class have non abstract methods PHP?

An abstract class can contain abstract as well as non abstract methods.

How define abstract method in PHP?

Abstract classes and methods are when the parent class has a named method, but need its child class(es) to fill out the tasks. An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared, but not implemented in the code.


Video Answer


2 Answers

See the chapter on Class Abstraction in the PHP manual:

PHP 5 introduces abstract classes and methods. Classes defined as abstract may not 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.

It means you either have to

abstract class absclass { // mark the entire class as abstract     abstract public function fuc(); } 

or

class absclass {     public function fuc() { // implement the method body         // which means it won't be abstract anymore     }; } 
like image 100
Gordon Avatar answered Sep 26 '22 06:09

Gordon


I presume that remaining methods actually refers to the abstract methods you're trying to define (in this case, fuc()), since the non-abstract methods that might exist are okay anyway. It's probably an error message that could use a more precise wording: where it says remaining it could have said abstract.

The fix is pretty straightforward (that part of the error message is fine): you need to change this:

abstract public function fuc();

... into a proper implementation:

public function fuc(){
    // Code comes here
}

... or, alternatively and depending your needs, make the whole class abstract:

abstract class absclass {
    abstract public function fuc();
}
like image 32
Álvaro González Avatar answered Sep 22 '22 06:09

Álvaro González