Posts

Showing posts from March, 2020

What is Laravel localization? When we can us this?

When we are developing a multi-language site at that time we use laravel localization. Localization in Laravel in it's simplest term means changing the application's default language to the language preferred by the user. There are two different ways we can achieve localization in Laravel: 1. Using Short Keys 2. Using Translation Strings as Keys

What is the difference between self and static in PHP?

The main differences is that static allows late static bindings. One of the most useful scenarios that I found was for creating Base classes for Singleton Classes: class A { // Base Class     protected static $name = '';     protected static function getName() {         return static::$name;     } } class B extends A {     protected static $name = 'MyCustomNameB'; } class C extends A {     protected static $name = 'MyCustomNameC'; } echo B::getName(); // MyCustomNameB echo C::getName(); // MyCustomNameC Using return static::$name in the Base class will return what was statically attached when it was extended. If you were to use return self::$name then B::getName()  would return an empty string as that is what is declared in the Base class.

Why we need interface in PHP?

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 inheritance because a class can implement more than one interface whereas it can extend only one class. Interfaces are 100% abstract classes – they have methods but the methods have no ‘guts’. Interfaces cannot be instantiated – they are a construct in OOP that allows you to inject ‘qualities’ into classes .. like abstract classes. Where an abstract class can have both empty and working/concrete methods, interface methods must all be shells – that is to say, it must be left to the class (using the interface) to flesh out the methods. Interfaces allow you to define/create a common structure for your classes – to set a standard for objects. Interfaces solves the problem of single inheritance – they allow you to inject ‘qualities’ from multiple sources. Interfaces provide a flexible base/root s

What is autoload in PHP?

Autoloading is the process of automatically loading PHP classes without explicitly loading them with the require() , require_once() , include() , or include_once() functions. It's necessary to name your class files exactly the same as your classes. As of PHP 7.2.0 the __autoload() function has been deprecated. Now it is recommended to use the spl_autoload_register() for that purpose instead.