Home » Php » oop – Abstract keyword in PHP

oop – Abstract keyword in PHP

Posted by: admin July 12, 2020 Leave a comment

Questions:

Hey, I’m quite experienced with PHP but I have no idea what the keyword abstract does when it comes down to object orientated programming. Can anyone explain in plain english what it can be used for?

What situations would I use the abstract keyword in? How does it change the class/interface?

How to&Answers:

(Hope this is simple enough — I don’t think I can do better ^^ )

An abstract class can not be instanciated : you can only create another class that inherits from the abstract class, and instanciate that child class.

And if you declare some methods as abstract, those must be defined in the child class, for that one to be instanciable.

Answer:

Declaring a class abstract means that it must be subclassed in order to be used. An abstract class can not be instantiated. One can see it as an extended interface that might include implementation code (as opposed to an interface).

By declaring a method abstract, you force the sub class to implement the method.

Answer:

The definition is mentioned above, now I will try to give you an example:

“abstract” ensures that you follow a specific logic, e.g. a ticket’s material is ALWAYS “paper”, or a creditcard must always have a “code”.
This is important if you work in a big company which has strict standardisation or if you want to ‘force’ your developers to follow a specific structure, so their code won’t end up in a mess.

    abstract class ticket{

    public function material()
    {
        return 'Paper';
    }

}

abstract class creditcard{

    public function material()
    {
        return 'Plastic';
    }

    abstract function setCode(); // the ";" semicolon is important otherwise it will cause an error

}

class key extends ticket{

    public function getMaterial()
    {
        return parent::material();
    }
}

class anotherKey extends creditcard{

    public function setCode($code)
    {
        $this->code = $code;
    }
}

If we do not define the “setCode” method the parser will return an error on “new anotherKey

Answer:

Abstract classes are used to an actual a-kind-of-model relationship. This allows for example a database driver to map the hierarchy, in which aims to provide a common base class, the signatures for the methods of the actual driver classes. The implementation is then carried out in accordance with the predetermined signatures in the actual driver classes.

here is code example

<?php
abstract class AbstrakteKlasse {
  public abstract function methode();
}

class ImplementierendeKlasse extends AbstrakteKlasse {
  public function methode() {
    print "ImplementierendeKlasse::methode() aufgerufen.\n";
  }
}

$objekt = new ImplementierendeKlasse;
$objekt->methode();
?>

Answer:

Though you cannot instantiate an abstract class, you can declare concrete methods/properties/variables (in C#, AFAIK) which will be available to the derived class

class Program
    {
        static void Main(string[] args)
        {
            Dog a = new Dog();
           //concrete properties and methods in abstract base class 
          //are available to derived class
            a.Name = "SuperDog";
            a.EatFood();
            //And now calling Dog's method
            a.Speak();            
            Console.WriteLine(a.GetType());

        }
    }

    public abstract class Animal
    {
        public string Name { get; set; }
        public void EatFood()
        {
            Console.WriteLine("Eating..");
        }
    }

    public class Dog :Animal
    {
        public void Speak()
        {
            Console.WriteLine("Bow .. Bow");
        }
    }