How do I use the \Drupal
class in an standalone PHP file?
3 Answers
You can use drush in a shebang, if you have drush installed.
This is an example for a standalone php file, where you can access Drupal classes:
#!/usr/bin/env drush
$sitename = \Drupal::config('system.site')->get('name');
echo "This is the site $sitename\n";
Since most of the drupal core components are composer packages you can load them quite easily using composer:
composer require drupal/component_name
You just need to require the composer's autoloader into your PHP script and you can quite easily use these classes in your own scripts.