Tuesday, January 22, 2019

PHP Quick Help

Static late binding

As of version 5.3, PHP introduced a new feature called PHP static late binding. Basically, instead of using the self, you use the static keyword that references to the exact class that was called at runtime.

Traits

Introduced in PHP 5.4.0.

Inheritance makes the code very tightly coupled therefore makes the code hard to maintain.

To overcome this problem, as of version 5.4.0,  PHP introduced a new unit of code reuse named trait. Traits allow you to reuse a set of methods freely in many different classes that do not need to be in the same class hierarchy.

The trait is similar to a class but it is only for grouping methods in a fine-grained and consistent way. It is not allowed to instantiate a trait on its own.

To use a trait in a class, you use the use keyword. All the trait’s methods are available in the class where it is used. Calling a method of a trait is similar to calling an instance method.

Abstract Class

Can not be instantiated. Should contain at least one abstract method.

Shallow Copy & Deep Copy


$p2 = $p1; // $p2 & $p1 objects will point to same object, reference change only.


$p2 = clone $p1; //Creates a new object from $p1 object

Function & Method

Both are same, a set of statements grouped by a name. So that they can execute wherever we want by using that name. The only difference is that a method is written inside a class, so it's always part of the class and can be called by using the object of the same class. Functions are written outside the class or normally in a plain PHP file.

Magic Methods


Magic methods are nothing but class member functions that are automatically executed based on some other actions.


__set(), __get(), __construct(), __destruct(), __call(), __clone(), __toString(), __autoload()  are examples of magic methods.


__set() & __get() are executed when an object tries to access(read / write) a private or non existent property.



__call() is executed when an object tries to call a nonexistent method.

Interface


An interface is nothing but a class with some skeleton member functions(methods) which will help the programmers to implement the same methods in a different class which are unrelated. So this will allows you to model multiple inheritances so that a class can implement multiple interfaces whereas it can extend only one class.

Why should we use PHP interfaces?
There are several reasons that we should use interface in our PHP object-oriented programming:

By implement an interface, the caller of the object need to care only about the object’s interface, not implementations of the object’s methods. Therefore the implementations can be changed without affecting the caller of the interface.
An interface allows unrelated classes to implement the same set of methods, regardless of their positions in the class inheritance hierarchy.
An interface enables you to model multiple inheritances because a class can implement more than one interface whereas it can extend only one class.

No comments:

Post a Comment