Php interviews tips

Php interview questions for experienced programmer

1) What is the use of “Final class” and can a final class be an abstract?

The “Final” keyword is used to make the class uninheritable. So the class or it’s methods can not be overridden.
1final class Class1 {
2    // ...
3}
4
5class FatalClass extends Class1 {
6    // ...
7}
8
9$out= new FatalClass();
An Abstract class will never be a final class as an abstract class must be extendable.

2)Explain abstract class and its behaviour?


Abstract class is defined as a solitary entity, so other classes can inherit from it. An abstract  class can not be instantiated, only the subclasses of it can have instances. Following is one of the best examples to explain the use of an abstract class and the behavior of it.
01class Fruit {
02private $color;
03 
04public function eat() {
05 //chew
06}
07 
08 public function setColor($c) {
09  $this->color = $c;
10 }
11}
12 
13class Apple extends Fruit {
14 public function eat() {
15  //chew until core
16 }
17}
18 
19class Orange extends Fruit {
20 public function eat() {
21  //peel
22  //chew
23 }
24}
Now taste an apple
1$apple = new Apple();
2$apple->eat();
What’s the taste of it? Obviously it’s apple
Now eat a fruit
1$fruit = new Fruit();
2$fruit->eat();
What’s the taste of it? It doesn’t make any sense. does it? Which means the class fruit should not be Instantiable . This is where the abstract class comes into play
1abstract class Fruit {
2 private $color;
3 
4 abstract public function eat()
5 
6 public function setColor($c) {
7  $this->color = $c;
8 }
9}