Skip to content

eval-file seems to require global $wpdb; for some reason #12

@Flimm

Description

@Flimm

Take this example file:

<?php

echo $wpdb;

This causes the following error:

$ wp --debug eval-file example.php
# ...
PHP Notice:  Undefined variable: wpdb in example.php on line 3
PHP Stack trace:
PHP   1. {main}() /usr/local/Cellar/wp-cli/1.3.0/php/boot-fs.php:0
PHP   2. include_once() /usr/local/Cellar/wp-cli/1.3.0/php/boot-fs.php:17
PHP   3. WP_CLI\bootstrap() /usr/local/Cellar/wp-cli/1.3.0/php/wp-cli.php:23
PHP   4. WP_CLI\Bootstrap\LaunchRunner->process() /usr/local/Cellar/wp-cli/1.3.0/php/bootstrap.php:75
PHP   5. WP_CLI\Runner->start() /usr/local/Cellar/wp-cli/1.3.0/php/WP_CLI/Bootstrap/LaunchRunner.php:23
PHP   6. WP_CLI\Runner->do_early_invoke() /usr/local/Cellar/wp-cli/1.3.0/php/WP_CLI/Runner.php:923
PHP   7. WP_CLI\Runner->_run_command() /usr/local/Cellar/wp-cli/1.3.0/php/WP_CLI/Runner.php:63
PHP   8. WP_CLI\Runner->run_command() /usr/local/Cellar/wp-cli/1.3.0/php/WP_CLI/Runner.php:330
PHP   9. WP_CLI\Dispatcher\Subcommand->invoke() /usr/local/Cellar/wp-cli/1.3.0/php/WP_CLI/Runner.php:323
PHP  10. call_user_func:{/usr/local/Cellar/wp-cli/1.3.0/php/WP_CLI/Dispatcher/Subcommand.php:401}() /usr/local/Cellar/wp-cli/1.3.0/php/WP_CLI/Dispatcher/Subcommand.php:401
PHP  11. WP_CLI\Dispatcher\CommandFactory::WP_CLI\Dispatcher\{closure}() /usr/local/Cellar/wp-cli/1.3.0/php/WP_CLI/Dispatcher/Subcommand.php:401
PHP  12. call_user_func:{/usr/local/Cellar/wp-cli/1.3.0/php/WP_CLI/Dispatcher/CommandFactory.php:81}() /usr/local/Cellar/wp-cli/1.3.0/php/WP_CLI/Dispatcher/CommandFactory.php:81
PHP  13. EvalFile_Command->__invoke() /usr/local/Cellar/wp-cli/1.3.0/php/WP_CLI/Dispatcher/CommandFactory.php:81
PHP  14. EvalFile_Command::_eval() /usr/local/Cellar/wp-cli/1.3.0/vendor/wp-cli/eval-command/src/EvalFile_Command.php:36
PHP  15. include() /usr/local/Cellar/wp-cli/1.3.0/vendor/wp-cli/eval-command/src/EvalFile_Command.php:40

However, if I change the example file so that it looks like this:

<?php

global $wpdb;
var_export( $wpdb );

Then I get the expected output:

$ wp --debug eval-file example.php
# ...
wpdb::__set_state(array(
   'show_errors' => true,
   'suppress_errors' => false,
   'last_error' => '',
   'num_queries' => 8,
   'num_rows' => 0,
   'rows_affected' => 1,
   'insert_id' => 0,
# ...

My question is: why is the line global $wpdb; required here? Since the code is not within a function, shouldn't the global be accessible directly without requiring the line global $wpdb;?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions