PHP ABSTRACT CLASSES

Sugar babies

PHP ABSTRACT CLASSES 

              •An abstract class is used to extend a class that imports public , private , protected , and abstract methods.
             •Every abstract methods defined in the abstract class must be implemented inside the extended class. 
             •This is useful when we need to define a common structure between different classes.
             •An abstract class is like the normal class it contains variables it contains protected variables functions it contains constructor only one thing is different it contains abstract method.  
            •Abstract must contains the abstract method and those methods must be defined in its inheriting class.

SYNTAX FOR ABSTRACT CLASSES IN PHP :

abstract class class_name { //abstract or non-abstract methods. }

EXAMPLE FOR ABSTRACT CLASSES IN PHP :

<?php
abstract class ParentClass {
  // Abstract method with an argument
  abstract protected function prefixName($name);
}
class ChildClass extends ParentClass {
  public function prefixName($name) {
    if ($name == "Roshan") {
      $prefix = "Mr.";
    } elseif ($name == "Roshan") {
      $prefix = "Mrs.";
    } else {
      $prefix = "";
    }
    return "{$prefix} {$name}";
  }
}
$class = new ChildClass;
echo $class->prefixName("Roshan");
echo "<br>";
echo $class->prefixName("Roshan");
?>

OUTPUT :

Mr.Roshan 
Mrs.Roshan
Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send