Articles

Hooks into the bean lifecycle in the Ding container


Using XML, Yaml, and Annotations to execute code in your PHP components

Sometimes it's useful (or needed) to have the container call certain methods right before injecting (or giving away) our beans, and right before destructing it, as a way of setup-teardown or init-shutdown.

Ding provides a way to execute code in some of the points of the bean lifecycle:

  • After the bean has been assembled, and all its dependencies injected.
  • Right before calling the bean's destructor.

We'll see how to use them with the 3 drivers. In the following examples, the method "init" will be called as soon as the beans are completely assembled, and the shutdown method will be called when the container is destroyed:

Using PHP annotations execute code during the lifecycle of your component

You can do this in 2 ways. At the method level:

And at the class level:

Using XML execute code during the lifecycle of your component

Using YAML execute code during the lifecycle of your component