top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is Abstract (base) class in PHP OOP's?

+2 votes
401 views
What is Abstract (base) class in PHP OOP's?
posted Oct 22, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

The abstract classes and methods are used to create a model of minimum required methods which must be defined in normal sub-classes derived from an abstract class (with extends).
An abstract class is created with the abstract keyword.
An abstract class cannot be instantiated, can only be inherited by other sub-classes extended from it.

The abstract methods are declared with the abstract keyword, and cannot have an implementation, they simply declare the method's signature.

abstract public function methodName($arguments);

Abstract methods are only created in an abstract class.

  • Example:
    In this example is created an abstract class with a property ($name), an abstract method ( greetName() ) and a normal method ( setName() ).

    <?php
    // AbstractClass class
    abstract class AbstractClass {
    protected $name;

    // declare an abstract method
    abstract public function greetName($greet);

    // define a normal method
    public function setName($name) {
    $this->name = $name; // sets the value of $name property
    }
    }
    ?>

answer Oct 29, 2014 by Vrije Mani Upadhyay
Similar Questions
0 votes

Do u know about planning or implementing OOP methdology access to array methods in PHP in future. Like show code later:

$a = new Array();
$a->append("hello");
$a->shift();
$p = $a->pop();

$b = Array::fill(fill_char="*", count=20);
$b->pop();
+1 vote

Can someone provide the details about class and objects in oops?

+1 vote

I am working on an OOP project, and cannot decide which way to follow when I have to write a simple function.

For example, I want to write a function which generates a random string. In an OOP environtment, it is a matter of course to create a static class and a static method for that. But why? Isn't it more elegant, if I implement such a simple thing as a plain function? Not to mention that a function is more efficient than a class method.

So, in object-oriented programming, what is the best practice to implement such a simple function?

...