<?php
abstract class ParentClass {
abstract public function someMethod1();
abstract public function someMethod2($name, $behavior);
abstract public function someMethod3() : string;
}
?>
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Parent class
- - - - - - - - - - - - - - - - - - - - - - - - - - - - */
abstract class Dwarfs {
public $name;
public function __construct($name) {
$this->name = $name;
}
abstract public function intro() : string;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Child classes
- - - - - - - - - - - - - - - - - - - - - - - - - - - - */
class Doc extends Dwarfs {
public function intro() : string {
return "$this->name<br>Leader of the group, not too good at speaking.";
}
}
class Bashful extends Dwarfs {
public function intro() : string {
return "$this->name<br>The shy one.";
}
}
class Grumpy extends Dwarfs {
public function intro() : string {
return "$this->name<br>Always annoyed and irritated, doesn't
like Snow White.";
}
}
class Happy extends Dwarfs {
public function intro() : string {
return "$this->name<br>Fat, jolly, friendly, spreading joy to everyone.";
}
}
class Sleepy extends Dwarfs {
public function intro() : string {
return "$this->name<br>Always tired, most observant.";
}
}
class Dopey extends Dwarfs {
public function intro() : string {
return "$this->name<br>Annoying, silly, the comic relief.";
}
}
class Sneezy extends Dwarfs {
public function intro() : string {
return "$this->name<br>Likes to have fun, has never
ending case of hay fever that makes him sneeze.";
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Create objects from the child classes
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$Doc = new Doc("Doc");
echo $Doc->intro();
echo "<br><br>";
$Bashful = new Bashful("Bashful");
echo $Bashful->intro();
echo "<br><br>";
$Grumpy = new Grumpy("Grumpy");
echo $Grumpy->intro();
echo "<br><br>";
$Happy = new Happy("Happy");
echo $Happy->intro();
echo "<br><br>";
$Sleepy = new Sleepy("Sleepy");
echo $Sleepy->intro();
echo "<br><br>";
$Dopey = new Dopey("Dopey");
echo $Dopey->intro();
echo "<br><br>";
$Sneezy = new Sneezy("Sneezy");
echo $Sneezy->intro();
?>
<?php
abstract class Dwarfs {
abstract protected function addName($name);
}
class ChildDwarfsClass extends Dwarfs {
public function addName($name) {
if ($name == "Peter Diklage") {
$add = "Mr.";
} elseif ($name == "Erica Schmidt") {
$add = " married with Mrs.";
} else {
$add = "";
}
return "{$add} {$name}";
}
}
$class = new ChildDwarfsClass;
echo $class->addName("Peter Diklage");
echo $class->addName("Erica Schmidt");
echo '.';
?>