Difference between final, Static and const keyword

  • By using final methods or classes can not be modified by a child class. This prevents class inheritance, method-overriding and/or redefinition of methods.
  • By using static class or methods or properties as a static value so that you have access to them without instantiating an object. These are shared between parent and child-classes.
  • By using const you can create a constant value for a class. The constant values will get changed and can NOT be changed by a method in either parent or child-class.

Difference between abstract and interface class

  • An interface is an empty shell and there are only the signatures of the methods, which implies that the methods do not have a body. The interface can’t do anything. It’s just a pattern.
  • An abstract classes can maintain abstract methods and non abstract methods and In abstract classes, we can create the variables and we can use any access specifier. Abstract class does not support multiple inheritance and abstract class should have one abstract method.

What is Traits?

PHP support only single inheritance and now traits solve a problem you can achieve multiple inheritance by using Traits.
A Trait is similar to a class and It helps developers to reuse traits methods freely in several independent classes in different class hierarchies. Traits reduces the complexity, and avoids problems associated with multiple inheritance.

<?php
trait TraitName {
    //methods
}
?>
To use a trait in a class by using “use” keyword
<?php
class NewClass {
   use TraitName;
}
?>

News Reporter

Leave a Reply

Your email address will not be published. Required fields are marked *

*

code