I've just started having a play with Kohana, coming from CodeIgniter and straight php. I was wondering why Kohana uses the before()
and after()
functions rather than normal constructors and destructors?
Asked
Active
Viewed 1,566 times
4

Cubed Eye
- 5,581
- 4
- 48
- 64
-
4Because doing so allows a greater level of control. – hakre Jan 02 '12 at 10:54
1 Answers
8
There's a subtle difference between the 2:
The constructor
and destructor
are called when an instance is created and destroyed.
The before
and after
methods are called before and after an action of a controller is executed.
Besides that, you can't guarantee the constructor
and destructor
are called before and after the action is executed, but you can guarantee that for the before
and after
methods.

SpadXIII
- 906
- 4
- 5
-
4Unless your application is not unrecoverable broken the destructor is garantueed. On the other hand `after()` is not called too, when the app is broken before it is called. – KingCrunch Jan 02 '12 at 11:26
-
I guess I meant you can't guarantee the constructor/destructor being called before/after the execution of the action. I'll change this a bit to clarify more what I meant. – SpadXIII Jan 02 '12 at 11:54
-
It's also guaranteed, that the constructor is called before `before()` and the destructor is called after `after()` :D But I think I can imagine, what you mean: You have less till no control on _when_ `__destruct()` gets called. However, I upvoted already, anyway, because the interesting parts are clarified. – KingCrunch Jan 02 '12 at 12:06
-
That was my point exactly: you have no control over exactly when the constructor and destructor are being called. Before and after are called directly before and after the action :) – SpadXIII Jan 02 '12 at 13:22
-
Also, the parameter list for the constructor has changed since 3.0 but I assume the before method won't ever have parameters. Some people had issues when upgrading which they could have avoided by using the before method. – Darsstar Jan 02 '12 at 15:09
-
@Darsstar: optimistic assumption :) As I see the Kohana team is pretty radical in upgrades. It wouldn't surprise me much if everything changed in the next version :) – ZolaKt Jan 03 '12 at 12:20