Tinkering with Tinkerwell

I’ve bought Tinkerwell a while back but I could never seem to find the time to set it up and start using it.

During a weekend with more free time than usual I finally decided to set it up.

Writing a custom driver

As I needed custom support for the project I’m currently working on, I’ve quickly started writing a custom driver for it. Writing a driver is a quick and painless process, however it wasn’t long until I ran into an issue.

The way drivers are designed in Tinkerwell, there’s a method called bootstrap where you can load all your application specific start-up code.

The issue I ran into is around global variables. The PHP documentation mentions that:

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward.

This means that all variables defined in the included files are available inside the bootstrap function scope. However, trying to use any of those variables using global ends up resulting in those variables not being found, as they don’t exist in the global scope.

Variable scope and include

There is an work-around available, if you can extract all the variables that are used globally, you can declare them as global before including the bootstrapping files. This way assigning values to those variables ends up updating them in the global scope and further global calls work as expected. That is however tedious, as I couldn’t find an easy way to extract the variables used globally in the project.

As the PHP code that Tinkerwell runs is available in .phar format, I’ve ended up modifying the code to have the driver provide a list of files for inclusion and directly include them in the global scope. I’ve also created an issue to hopefully add support for this use-case in Tinkerwell.

Hopefully this provides a bit of insight into how variable scoping works with including files as this puzzled me for a second or 5 when I ran into it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s