New install - Undefined type 'context_system'.

New install - Undefined type 'context_system'.

by Dylan Barwick -
Number of replies: 8

I just installed Moodle on my mac using docker and ddev ( https://ddev.readthedocs.io/en/stable/users/quickstart/#moodle ) and it seems to function in that I can browse the administration pages, visit the page defined by the greetings plugin etc but when I try to edit the code in my IDE (VS Code) the reference to `context_system` is underlined in red and the message I get from the intelephense plug-in is "Undefined type 'context_system'."

I've checked other instances of context_system::instance() and that class cannot be found. 

I reinstalled the codebase with a downloaded version of Moodle 4.4.3 and updated the database but the context_system class is still missing. I'm still working my way through the basic tutorials and I don't want to get too deeply into it without fixing this fundamental issue.

Any ideas?

Average of ratings: -
In reply to Dylan Barwick

Re: New install - Undefined type 'context_system'.

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
context_system is (unusually) defined as a 'class_alias' of 'core\context\system'. The alias is defined in lib/accesslib.php (around line 5000). The class is defined in the file lib/classes/context/system.php.

I think we should be using \core\context\system::instance() but nobody does. Anyway, check that the definition is there but I suspect the class_alias may be what's upsetting VSC.
Average of ratings:Useful (1)
In reply to Howard Miller

Re: New install - Undefined type 'context_system'.

by Dylan Barwick -
Thanks for the speedy response. I changed `$context = context_system::instance()` to `$context = \core\context\system::instance();` and it stopped complaining. I will come back to this at a later date and get a better handle on it but for now I'm happy that it works.
In reply to Dylan Barwick

Re: New install - Undefined type 'context_system'.

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

Presumably either the IDE or the IDE plugin is failing to detect the PHP class alias in lib/accesslib.php:

class_alias(core\context\system::class, 'context_system', true);

Try posting this in the general developer forum, hopefully someone with the same IDE set-up can help.

In reply to Leon Stringer

Re: New install - Undefined type 'context_system'.

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Oddly, I *do* have the same IDE setup and I've never noticed a problem.
 
I'll just move this discussion as it's definitely a developer thing.
In reply to Dylan Barwick

Re: New install - Undefined type 'context_system'.

by Hugo Ribeiro -
Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
I also get this behaviour using VScode. Never understood why
In reply to Hugo Ribeiro

Re: New install - Undefined type 'context_system'.

by Dylan Barwick -
It seems that VS Code doesn't always recognise class aliases. I named the class explicitly using $context = \core\context\system::instance(); (see previous response above) and it seems to work.
In reply to Dylan Barwick

Re: New install - Undefined type 'context_system'.

by Mark Sharp -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

That would appear to be a known issue with intelephense in VS Code: https://github.com/bmewburn/vscode-intelephense/issues/600

In reply to Mark Sharp

Re: New install - Undefined type 'context_system'.

by Mark Sharp -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Apparently PHPStorm handles class_alias, and class_alias is only analysed at run time. One suggestion I saw said to create the original class and extend it with the new one. Did a little test (only in the IDE, not a running Moodle) and that appears to work. E.g. in /lib/accesslib.php under the class_alias add:

class context_system extends core\context\system {}

If this isn't going to be fixed in intelephense in vscode this is going to be a really big annoyance, unless we all start using PHPStorm.