search

Home  >  Q&A  >  body text

PHP - exception class not found - wrong namespace

After instantiating moodle via "external"

require_once('../config.php');

In an SSO scenario within the application (i.e. MRBS), when looking up whether the currently logged in user has certain capabilities for a specific block, I get the error: Exception - class "MRBS\Session\context_block" not found :

if (has_capability('moodle/block:edit', context_block::instance($blockid)){}

I guess it’s because the namespace is set to namespace MRBS\Session;

How to correctly quote context_block::instance()?

Moodle functions apparently work (e.g. require_login(), has_capability). Thanks

P粉770375450P粉770375450244 days ago511

reply all(1)I'll reply

  • P粉642920522

    P粉6429205222024-03-23 09:09:32

    You need to write:

    if (has_capability('moodle/block:edit', \context_block::instance($blockid)) {}

    Use the "\" character to declare context_block in the top-level namespace.

    Or you need to put the following at the top of the file:

    use \context_block;

    I personally prefer the first option, but that's usually a matter of personal preference.

    reply
    0
  • Cancelreply