$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>rest api - Access WP_REST_Server from within plugin namespace|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

rest api - Access WP_REST_Server from within plugin namespace

matteradmin9PV0评论

I am new to OOP and developing plugins. I have developed a plugin using the Plugin-Boilerplate with namespace. I expanded the boilerplate to include a REST folder and class so I could create custom routs/endpoints. It is working; however I cannot use the WP_REST_Server object. I get the following error: ..class-wp-rest-server.php does not exist.From my limited knowledge of oop my guess I it is not in my namespace. But how do I include/access it? My guess is there is a simple mechanism that is obvious to experienced OOPs. Thanks.

I am new to OOP and developing plugins. I have developed a plugin using the Plugin-Boilerplate with namespace. I expanded the boilerplate to include a REST folder and class so I could create custom routs/endpoints. It is working; however I cannot use the WP_REST_Server object. I get the following error: ..class-wp-rest-server.php does not exist.From my limited knowledge of oop my guess I it is not in my namespace. But how do I include/access it? My guess is there is a simple mechanism that is obvious to experienced OOPs. Thanks.

Share Improve this question asked Feb 1, 2019 at 14:47 dsjdsj 638 bronze badges 2
  • 1 Don't know if this is precisely the problem you're having, but generally if you're in a namespace and want to access something in the global namespace you prefix it with a backslash. i.e. throw new \Exception( ... – tmdesigned Commented Feb 1, 2019 at 17:00
  • I though I had tried that with no effect. Tried it again and it worked. – dsj Commented Feb 1, 2019 at 17:25
Add a comment  | 

1 Answer 1

Reset to default 4

WordPress (version 5.0) doesn't use namespaces for it's classes, so try to use the global one:

\WP_REST_Server

to avoid assuming it's in your custom namespace.

One of the reason for this is that WordPress supports PHP 5.2.4+ that doesn't support namespaces. The minimum is PHP 5.3.3+, if I remember correctly, to support it.

Later this year (2019) WordPress plans to bump the minimum required PHP version to 5.6 and then to 7 at the end of the year. So after that, WordPress namespaces could be possible, but I'm not aware of any such planned changes though :-)

Post a comment

comment list (0)

  1. No comments so far