php - Best Practices for __get() and __set() -


__ get () and __ set () on it To use the variable, I want to get input on how they are used in general, I am thinking that when the best time to use the overloading function, and where you have used one (if you have).

Just to be clear, we are talking about these tasks:

Using lazy model gates (__get ())

I do not remember using magic, magic methods are often in my app, but I miss a situation where __ gets () was very helpful.

In those days, I was developing an application in the KP PHP framework which was launched in many models and specific controllers, even though the method was one or two of them (Only the cake was working). So I decided to turn that lazy model into lazy (to load the models used for the first time).

What I did is very simple __ get () function with a specific name that was looking for a model and loaded it 3-4 lines of code Was like. I have defined that in AppController (all the KPPHP classes come out of that controller) and suddenly my app used speed and used less memory.

I took it forward later and loaded the lazy components in the same way.

using dynamic model methods (__call ())

Another good example, from CakePHP, how the model searches for cakes Basically you have two methods for it : find () and findAll () in each model, but you can also use methods like findBy & lt; FieldName & gt; () and findAllBy & lt; FieldName & gt; () .

For example, if you have a DB table

  notes (id, date, title, body)  

Make cake models for You can use the findById () , findByTitle () and similar methods. You only need the Camel CBS DB field and you can search very quickly on any area.

The cake uses the __ call () magic method. This method is called if you are trying to execute a method that does not exist and then It only runs find () or findAll () and the parameter is very simple and you can actually get many benefits.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -