PHPStorm: Scratch Files as Tinker

manish maharjanmanish maharjan
3 min read

As a developer, we often need quick test code snippets, library exploration, or debugging specific application parts. Observing the history of such tasks, Laravel's Tinker seems the most preferred choice.
However, using PHPStorm as an IDE for development, one can leverage the ability of scratch files to achieve alike functionality with greater convince and get the benefits of an integrated environment.

In this blog, we will explore how to make use of PHPStorm Scratch Files in an effective way simulating the tinker-like experience with examples.

Environment setup

We will be using Laravel 's reference for the simulation of scratch file power. Here is the directory structure and some initials code setup you might have:

/new-laravel-project
|-- app
|-- bootstarp
|-- config
|-- database
|-- public
|-- resources
|-- routes
|-- storage
|-- tests
|-- vendor

Step-by-step Guide

  1. Open PHPStorm and Navigate to Scratch Files

  • Open PHPStorm

  • Go to File -> New Scratch File -> PHP

  • A new scratch file will be created with the name something like this 'scratch.php' under Scratches and Consoles -> Scratches

  1. Bootstrap your Laravel application in the scratch file

    Copy and paste the following code into your scratch file:

const BASE_PATH = '/new-laravel-project'

require_once BASE_PATH.'/vendor/autoload.php'

use Illuminate\Foundation\Console\Kernel;

$app = require BASE_PATH.'/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();

//Your code goes here
  1. Writing Test Code:

    Now you are free to write any code you want to test within this file. For example, if you want to test a simple database query, something like this can be done:

$user= \App\Models|User::find(1);
echo $user->name;
  1. Running the Scratch File

    • Right-click on the scratch file and select Run scratch.php

    • PHPStorm will execute the script, the output can viewed from the Run tool window:

Benefits of Using PHPStorm Scratch Files

  1. Integrated Environments:

    PHPStorm provides a powerful, integrated development environment with features like autocompletion, debugging, and other advanced features to enhance productivity

  2. Easy to manage:

    it is easy to create scratch files without affecting the main codebase. They remain ideal for serving with experiments and temporary testing purposes only.

  3. Access to Project Context:

    The entire project context is accessible in scratch files, which helps the developer test any part of the application without the need for separate scripts or environments.

  4. No need for a REPL:

    While Tinker serves as a great REPL(READ-EVAL-PRINT-LOOP), it might not always integrate well with the IDE. Whereas, Scratch files can fulfill that part with interactive experience co-working with PHPStorm's features.

References


Conclusion

To conclude, we can expect a versatile and powerful way of testing and debugging PHP code within a familiar environment with PHPStorm Scratch files. By Laravel project context setup in a scratch file, the developer can effectively use it like with Tinker, instantly aiding in rapid experimentation and problem-solving.

Try incorporating scratch files into your project workflow and experience the seamless integration and efficiency brought to your development process.

I hope this blog was helpful ๐Ÿ˜Š. Please feel free to ask me any other questions you may have. I am always happy to help ๐Ÿค—.

Happy coding with PHPStorm.

10
Subscribe to my newsletter

Read articles from manish maharjan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

manish maharjan
manish maharjan