PHP OOP: Traits

What is Traits?

  • Code is shortened by using Traits.
  • One code is used everywhere just by writing the trait function.

Example :

Output:

Multi traits use example:

Output:

Tagged : / / / / / / / / / / / / / /

PHP OOP: Late Static Binding

Late static Binding:

  • Late static binding helps to override name by using static in echo keyword.
  • It helps lot for writing minimum coding.
  • The message of the base class is printed using the self keyword and the message of the derived class is printed using the static keyword.

Example to printing the message of base class:

Output:

Example of printing the message of derived class:

Output:

Tagged : / / / / / / / / / / / /

PHP OOP : Static Members

What is Static Members ?

  • Static properties and methods can be used without creating an instance of the class.
  • The static keyword is used to declare properties and methods of a class as static.
  • Object not create.
  • Stack keyword use before access modifier.
  • Inside class use self::(use static method).
  • inherit use parent class name ::(properties).

Example :

Example Constructor :-

Tagged : / / / / / / / / / / / / /

PHP OOP : Interfaces

What is Interfaces?

  • Interfaces help us to use multiple classes in a single class inherit.
  • Interfaces are declared with the interface keyword.
  • Derived class is called derived class.
  • object not be created in interface.
  • Property should not be defined in interface(class).
  • All interface methods must be public.
  • All methods in an interface are abstract.
  • To implement an interface, a class must use the implements keyword.

Example :-

Tagged : / / / / / / / / / / / / / /