Computed properties and methods in PHP

Sometimes, it’s handy if we have an ability to create property and method names from another properties just like what we have in JavaScript. In another words, a property or method name which can be set and used dynamically. For instance, a normal property can be set with a statement in PHP like so.

» Read More

Multiple constructors in PHP

Constructors are a really important part of the class-based object oriented programming. Through constuctors, a newly created objects initialize properties automatically before it is usable. In PHP, a constructor is a method named __construct(), which the keyword new automatically calls after creating the object. Constructors can also accept arguments, in which case, when the new statement is written, you also need to send the constructor arguments for the parameters.

» Read More

Bookmarks feature in PhpStorm is truly a saviour

I use PhpStorm for all my projects related to PHP for years now and I can’t imagine myself going back to another IDE. Period. The amount of features it provides for modern PHP developement is impeccable. There are tonnes of features though, so it’s rather overwhelming if you’ve just started using it and you want to learn all at once.

» Read More

Remembering what spaceship operator do on comparison in PHP

PHP has introduced an operator called “spaceship opearator” (<=>) with the release of PHP7. What this spaceship operator do is compare two expressions i.e. its two operands, let’s say $a and $b, and returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b.

» Read More

Using register_shutdown_function() instead of desctructor in PHP

The usual and might be the most used way of cleaning the object is to use the good old __destruct() magic method in PHP. The magic method proves to be good in most of the cases. But there might be some scenario where even the __destruct() method will fail. For instance, a scenario where your PHP script exceeds the maximum execution time, and terminates thusly. And a fatal error would occur called Maximum execution time of 20 seconds exceeded in - on line XX.

» Read More

Convert request parameters to boolean in Laravel

Sometimes, you might want to convert some of the request parameters to boolean. For instance, take a checkbox field. Unless and until, it hasn’t been checked it won’t be passed through to the request. In such a case, it would be beneficial to convert such inputs to boolean.

» Read More

Some lesser known facts of Traits in PHP

Traits in PHP is a way of re-using the code. Basically, Traits are assistive copy-paste mechanism provided by the language itself. Using Traits, developers can reduce the limitations of single inheritence based languages such as PHP. I have written a dedicated article about it if you want to check it out.

» Read More

Add view within another view in Blade Laravel

Sometimes, all you need to do is to add a Blade view into an another view. For instance, when you want to add JavaScript and CSS files as a separate Blade files which will contain the related content respectively. So, maybe you’ve a JavaScript blade file called view-js.blade.php which wraps the JS code within <script> tag and a CSS blade file called view-css.blade.php which wraps the CSS code within <style> tag. And all you want to do is add these to an another blade file called view-show.blade.php.

» Read More