July 3, 2020
The switch statement in PHP is great when you want check condition on the same expression without incorporating many
if statements altogether.
June 22, 2020
Up until now, or precisely before PHP 8, whenever you were unsure about which type to use for the propperty or return type, you’d leave it without assigning any type. And if you’re using an IDE such as PhpStorm, the docBlock would mark those properties as
mixed type like so.
June 16, 2020
Refactoring is important in programming things. It is a process of restructuring existing computer code—changing the factoring—without changing its external behavior. It makes code more readable, structured and easy to process.
June 13, 2020
Who would’ve thought that PHP would have to wait till its version 8 or rather 25 years after its inception, to get a truly native and understandable function to check if a string is contained in another string?!
June 10, 2020
The usual way of handling the exception is by requiring the
catch block to catch the exception (thrown from the
try block) to a variable like so.
June 9, 2020
Wouldn’t it be nice if you don’t have to declare the class property over and over again just to use it across the class? Currently, you’d do it by first declaring it…
June 4, 2020
Have you ever wanted a feature where you would only want to call a method or fetch a property on the result of an expression if it is not
null? So, for instance, check the following code.
June 1, 2020
With PHP 5, the core team had introduced type declarations (also known as type-hinting) that allow functions to require that parameters are of a certain type at call time. If the given value is of the incorrect type, then an error is generated: in PHP 5, this will be a recoverable fatal error, while PHP 7 will throw a TypeError exception.
May 12, 2020
May 8, 2020
If you’ve worked with modern frameworks such as Laravel, you might have seen this keyword
::class be used extensively throughout the application.
May 4, 2020
When PHP 7.0 released, it has added many nice things in PHP’s toolbelt of utilities. One of the things among this was Null coalescing assignment operator (??).
May 4, 2020
If you look at PHP’s core helper functions, they aren’t very intuitive to use and are pretty inconsistent at times. For instance, the array_map function is used to apply the callback to the elements of the given arrays. Here’s how its definition looks like.
April 17, 2020
I recently stumbled upon this ghost feature of PHP using which you can actually use streams in order to post data using a HTTP POST request.
April 15, 2020
Sometimes all you need is to define and call function at the same time and only once throughout the scope. Such functions are called as Immediately Invoke Function Expression (IIFE) also known as Self-Executing Anonymous Function.
April 1, 2020
Exceptions are really useful when you want to handle some situations which can not be handled gracefully otherwise. So, using exceptions, you can handle certain sceanrios by showing a nice error message. Take the following example for instance.
March 30, 2020
March 27, 2020
Have you ever felt the class properties that you’re using in the constructor are essentially repeated multiple times? i.e At the declaration, in the constructor parameters and while doing assignment in the constructor. For instance, take the following example.
March 3, 2020
March 2, 2020
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.
February 20, 2020
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
$b, and returns -1, 0 or 1 when
$a is respectively less than, equal to, or greater than
February 18, 2020
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.
February 10, 2020
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.
January 31, 2020
Exception handling is an essential part of the software development workflow. It is used to handle the runtime errors so that the normal flow of the application can be maintained.
January 26, 2020
There’s this little trick in PHP that I got to know about today. The trick basically is, you can loop through all the class properties having the “public” visibility just by iterating over the class object using loop construct such as
January 24, 2020
Working on an application which received user signups and let’s suppose it’s built on top of PHP, you want to validate that the email the user enters is valid. Sure, you’ll check that the email entered is a “syntactically” valid one by using one of these methods.
January 15, 2020
Sometimes, it’s convenient when you could get to access class objects as arrays. For instance, the time when one of the class properties is of type array and you want to manipulate it just like you’d do with an array without exposing it during object creation. I’ve included a real world example of where this could be useful, at the end of the article. So, read on!
December 18, 2019
Working with PHP, it would be pretty obvious you’d be worried about the performance of your application. And because of this anxiety, you start to optimize trivial things such as replacing double quotes(“) to single quotes(‘), use
echo instead of
print_r, or using static methods in order to gain those little performance gains during the initial few days of your application development itself. Optimizing these thigs is good and all.
December 16, 2019
In this short article, I’m going to explain what method chaining is and how it can prove to be useful as an object oriented design pattern.
December 7, 2019
PHP 7.4 is finally released a few days ago. It’s a fourth feature update to the PHP 7 series and it comes with a very interesting set of features which I’m going to talk about in this article. Some of are very long overdue such as unpacking arrays, typed properties and so forth. However, these all comes with some sort of caveats. Let’s talk about all of the new features in details.
December 5, 2019
Jetbrain’s PhpStorm is my goto IDE when it comes to the PHP developement. Be it Laravel, Symphony or Magento. It’s like a gold standard. The kind of intelligent features it offers is unmatchable and almost irrplaceable by any IDE of this level.
November 26, 2019
November 20, 2019
November 12, 2019
October 30, 2019
In this article, I’m going to discuss about the union types which are going to come in future versions of PHP through this RFC. To understand why and how are union types important in PHP, let’s first understand what are union types.
September 29, 2019
PHP is funny sometimes. There are many things in PHP which behaves differently instead of the way you think might work. I recently stumbled upon one such thing while working on one of my projects.
September 2, 2019
Passwords are an integral part of today’s authentication based web applications. In fact, Passwords have been used since ancient times. Sentries would challenge those wishing to enter an area to supply a password or watchword, and would only allow a person or group to pass if they knew the password. In modern web application passwords are used in combination with usernames to authenticate users. So, basically, we’d need to fields in order to store username and password for each user both of which are supplied by user at the time of signing up to the web application.
August 22, 2019
In object oriented programming, there are two ways using which classes can achieve polymorphic behaviour: “Inheritance” & “Composition”.
August 13, 2019
Some of the PHP’s core functions are designed in such a way that they do not handle exceptions in a better way. Instead, they just return
false if there’s any error. This makes developers to handle those error themselves. Take the example below.
August 7, 2019
The inheritence tree in PHP quickly gets dirty if you’re using mix of
non-static methods into your classes and the inheritence is deeply nested. Take from example here.
July 31, 2019
Have you ever stuck in a situation where the code that you’ve wrote uses foreach to iterate over a set of data into an array and which ultimately caused you to exceed a memory? Well, I’ve been to. Many times.
July 25, 2019
June 10, 2019
In PHP, Invokables refer to any class that may be instantiated without any constructor arguments. In other words, one should be able to create an instance solely be calling
new $className(). To implement an invokable class, one needs to use __invoke() magic method of PHP. Before we understand how invokable exactly works, let’s take a look why invokable classes even exists in PHP.
May 2, 2019
With the introduction of scalar type declaration and return type declaration in PHP 7.0, the language’s type system got improved at some extent. Although it’s great to have some layer of strictness, it’s still missing the support to declare typed properties. But from PHP 7.4, it seems, it’s going to change because according to this accepted RFC, PHP 7.4 will be getting support for first-class property type declarations.
March 29, 2019
February 22, 2019
Today, while working with a project which happens to be bulit on top of Magento 2, I needed to find a way to get the list all the admin users as an array.
February 9, 2019
While developing applications(not necessarily with Laravel), you come across the situation where you need to implement certain functionality. In such cases you shouldn’t want to reinvent the wheel if the functionality is been implemented by someone as a package, accepted well by the community and is ready to be consumed. It’s a no-brainer. I use to follow the same approach and today in this article, I’m going to list out some of my favorite Laravel packages which have made my experience with development in Laravel delicious.
January 17, 2019
PHP has evolved greatly as a language over the years. From a simple functional scripting language which initially started by Rasmus Lerdorf as his hobby project to supporting object oriented programming features PHP has come a long way. Although, PHP has been implementing and improving features of object oriented programming in its every release, there is this one feature which is missing and I’ve always wondered why it is not there in PHP all these years. Yes, I’m talking about multiple inheritance.
January 9, 2019
When working on a project, there comes a time when you need to automate certain tasks. In such cases, it is better to use a CLI tool which can be run from the command prompt because using a web APIs for certain tasks is cumbersome.
December 21, 2018
There comes times when you wouldn’t want your end users staring at white screens or that intimidating loaders for a long time. For instance when user registers to your site, you must have configured your website to send a welcome email or a confirmation email upon registering. So, that operation should be snappy and for that purpose you’d actually want to reduce the amount of time your app takes while sending the email to the user. Laravel Queues comes to the recue for achieving such kind of time consuming tasks.
December 8, 2018
PHP development team has just released PHP 7.3 with general availability. This release brings general improvements along with some new features. Even though this is a stable release, the team hasn’t provided concrete migration guide if you want to migrate from the older PHP versions. Maybe they will release those on a later date.
October 30, 2018
September 5, 2018
Composer is the goto method of adding dependencies in modern PHP application. You search for a library for a concerned functionality and you’ll probably find the one in the large database of the Pacakgist. But there comes a time where you’d want to use a library which is private and should be only available to you. Well, the good news is, you can achieve this using this trick.
August 23, 2017
Accessors and mutators allow you to format Eloquent attribute values when you retrieve or set them on model instances. I’ll explain how you can you use them into your app.
August 17, 2017
Today, while working with one of my projects(which is built on top Laravel) I bumped into the situation where I needed to get associated model’s data for one of the models. So here, I had two approaches in Laravel to accomplish this which are basically called Eager Loading: