PHP - exception class not found - wrong namespace
P粉770375450
P粉770375450 2024-03-22 12:41:34
0
1
520

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粉770375450
P粉770375450

reply all(1)
P粉642920522

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template