Laravel’s biggest secret is revealed – Magic Methods

Tram Ho

Hello friends! As you know, Laravel is a new framework but has completely outperformed other senior PHP frameworks in terms of popularity. So have you ever asked WHAT, what makes Laravel so prominent?

In this article, I want to introduce to you the characteristics of PHP that play an important role in building such a great Laravel framework, not to mention PHP’s Magic Methods .

So what is it ‘magic’ like that, let’s find out in this article!

What are Magic Methods?

First of all, you must understand that magic methods not exclusive to Laravel, but are available in any PHP application. Because I am working with Laravel Framework, so I would like to allow this article to give examples related to this framework.

Magic methods are special methods for customizing events in PHP. Simply put, it provides an additional way to solve a problem. Magic methods are used to handle objects in OOPS.

Magic methods will never be called directly, you can call magic methods behind the scenes .

There are currently 15 magic methods :

If you have been working with OOPs PHP in general, Laravel in particular, you have come across the __construct() method somewhere. The __contruct function will automatically be called when we instantiate an object (also called the constructor). So you’ve used the method magics , right?

You will also notice that all magic methods have a prefix __ .

This article, I can not introduce all the magic method , I just mentioned 4 functions: __get() , __set() , __call() , __callStatic() used in Laravel codebase. If you are interested in other functions, you can find out more here .

How has Laravel used ‘magic’?

__get ()

In Laravel’s Model is really special. They do not store attribute data as a Class attribute directly, but one of them has a protected $attributes , which is an array that stores the Model’s attribute attributes.

Differences between usage between the class in plain PHP and the Model class in Laravel

We can see that two working examples produce identical results. However, in Laravel attribute attribute not stored as in PHP, instead all attribute attribute are defined in an attribute named $attributes . So why when accessing the needed properties, they can return the right data: -? : -? : -?

Simply because behind the road there is always a __get magic method

Everything looks good!

We should note that the __get() magic method is only called when we access non-existent or protected ( private or private ) properties in the Class. To clarify this, let’s look at the example below:

There are many more things that Laravel does when the __get() magic methods are called, details can be found here .

__set ()

The __set() method is called when we pass data to a nonexistent property or property ( protected or private ) in the Class.

Let’s read on for an example to see the difference between the usage between the pure PHP class and the Laravel Model class!

In the example, we are trying to assign the value ‘Quan’ to the attribute attribute firstName does not exist in the Class that is defined in the $attributes .

You see the title is __set() , many of you probably guessed that the magic of the __set() function to handle it, but let’s try it out: -? : -? : -?

That’s it, we have successfully implemented the basic use of the __get() and __set() magic methods in Laravel. I am trying to get a simple example so you can have a simple understanding of the Laravel works, details about __set() can be found here .

__call () & __callStatic ()

The __call() method is called when we call methods that are not accessible within the scope of an object. In Laravel, this magic method is used to create macro contained in PHP.

What is the task of macros? The nature of macros differs in different languages, but it all carries a common mission of performing a task we already have.

Now let’s see how we can create macro in plain PHP …

This program will run without error because the fullName method fullName not been declared in the User class. We get the message Call to undefined method User :: fullName ()

Now with the __call magic method, we can define an array that will contain the Closure function.

So we know how to create macro using the __call magic method.

With the __callStatic magic method, it works similarly to the _call magic method, but for phương thức tĩnh static .

You can refer to Macroable in Laravel [here]. ( Https://github.com/laravel/framework/blob/7.x/src/Illuminate/Support/Traits/Macroable.php )

Conclude

Above I have shown you how Laravel uses magic methods in their basecode. This article may seem a bit deep about the framework for those who are new to Laravel, but in general, if you are sure of PHP knowledge, it is not difficult at all. Hopefully through this article, you can understand more lines of code I wrote it is handled like.

Thank you for reading my article !!!

References

https://www.php.net/manual/en/language.oop5.magic.php

https://github.com/laravel/framework/blob/7.x/src/Illuminate/Database/Eloquent/Model.php

https://github.com/laravel/framework/blob/7.x/src/Illuminate/Support/Traits/Macroable.php

Share the news now

Source : Viblo