If you are just starting out as a PHP developer or you just want to correct some PHP code, then you will eventually come across the -> symbol.
There are also a couple of other symbols that look similar, such as =>.
We take a look at these arrow symbols in PHP code which play an important role, and find out what the -> operator means.
What’s -> In PHP?
-> in a PHP script is referred to by developers as the object operator. It is also known as the single arrow operator.
In PHP code, operators are symbols that tell the interpreter or compiler to perform an operation, for example, a comparison between two values or an arithmetic calculation.
The -> arrow symbol is an access operator that is typically used for any access and call methods and any properties in PHP objects.
Together with the => symbol, the -> is also called an arrow function.
What Are Anonymous Functions?
Whenever a PHP developer defines a function in his PHP code, he typically assigns a name to it as well.
This name is important because it is used to call this function whenever he needs it later down in the code.
However, if the developer requires the function only in one place of the code, then it is easier to use this specific function without giving it a name.
These nameless-functions are called anonymous functions. They are typically only used when the function is relatively short.
What Are Arrow Functions?
Now that we know what anonymous functions are, it is easier to understand arrow functions. Arrow functions are essentially just shorter and simpler anonymous functions.
Arrow functions were introduced as part of PHP 7.4.
When you write arrow functions, then you replace the keyword function with fn, and you remove the return keyword completely.
This function contains only one simple expression, and that is the arrow function ->.
Another bonus of arrow functions is that any variables that are defined in the parent scope are also available without the need of a keyword.
Benefits Of An Arrow Function
As arrow functions are a short way of writing an anonymous function, they come with some great benefits for the developer.
An arrow function begins simply with a fn keyword, and then it contains only the one expression of the value within the function.
Arrow functions are a very easy way to save time as they are so much shorter than your standard anonymous function.
Functions with -> and => allow automatic access to any variables in their parent scope.
Limitations Of Arrow Functions
Despite their benefits, arrow functions also have a number of different limitations.
You cannot use an arrow function as methods or constructors. In addition, arrow functions also can’t use yield within body.
Arrow functions don’t have their own bindings with ‘super’ or ‘this’. This makes them impossible to use for bind methods and callback functions.
What is => in PHP?
=> is also an arrow function, and it is typically referred to as the double arrow operator.
Unlike ->, the => arrow symbol is an assignment operator which is used when a developer wants to create associative arrays.
You can find => operators between the array key and its associated value.
In simple terms, the key is on the left in enclosed quotes, then you place the => and assign the value next to it on the right.
You can give the key any name that you want but it is important that the key is in enclosed, single or double quotes.
The value in itself can but does not have to be in enclosed quotes. This will depend on the type of data you are using.
As an example, a developer always encloses a string value but you don’t have to do that with a boolean or integer value.
You can either use the value directly as part of the function or you can use it within a constant or variable, and then assign all of it to the key.
What Do Arrow Functions Look Like?
Arrow functions typically use the syntax of fn (arguments) -> expression.
All arrow functions have to begin the an fn keyword, and they are allowed to have only one expression.
As arrow functions are simple anonymous functions, they should only ever be used for simple operations.
When Should You Use Arrow Functions?
Thanks to their simplified syntax, arrow functions are a quick and easy way to write code.
You can use the simplified arrow function syntax with arrays, such as map(), filter() and reduce().
This makes these arrays much quicker to read and easier to understand within a large coding project.
You can also use arrow functions for callbacks.
When Should You Not Use Arrow Functions?
There are a number of different situations where you should not use arrow functions.
As arrow functions don’t have their own value and argument objects, they shouldn’t be used as event handler.
When an arrow function is used as an event handler, it will return an undefined value. This is because it doesn’t have its own value.
The best way to code an event handler is to use a regular function.
You should also avoid using arrow functions for object methods, prototype methods and in any functions that use an argument object.
An arrow function does not contain arguments objects, so you will need to use a standard function for this.
Although arrow functions, such as -> and => may be a quick and easy way to write anonymous functions, there are also situations when you should not use -> or =>.
In addition, these two functions also have different uses. The object operator, also called single arrow operator, -> is typically used to access properties of a class object and methods.
Developers also commonly use it for access to JSON object elements.
In comparison, the double arrow operator => is strictly an assignment operator which is used within associative arrays.
It helps to assign different values to keys when an array is created. => is commonly placed between the value and key.