Getting MODX version

Hi, Im currently using $this->modx->getVersionData()['version'] >= 3 for compatibility as noted here, but im getting a “PHP warning: Trying to access array offset on value of type bool” and when debugging, getVersionData returns true, what I’m missing? Im on MODX 3.0.3

1 Like

To my very basic understanding, this is an issue with PHP 7.4+ not with Revo 3 and that you might need to set a default value if the result is not an array.

As far as I can tell, the only way that would happen is if $modx->version is somehow set to something that is not an array and not null, and the getVersionData() is called more than once in a session.

The second time would set the version value to 1 instead of an array (which would cause your error when you try to access 1['version']).

I don’t know why its include_once rather than just include, which wouldn’t have that problem.

If you’re calling getVersionData() twice in your code, or setting the $modx->version value directly, not doing that might be a quick fix.

That method is never supposed to return anything that isn’t an array. So, while correct that PHP 7.4/8 have gotten more vocal about type errors, this is not a case where a default should be necessary but rather the core not doing what it is documented to do.

There is no need to use include over include_once because the result of the include_once is cached in the $version property. Including it a second time isn’t going to change the value, and it should not even get to the include_once statement a second time to begin with.

@camicase82: for some reason your core/docs/ file does not seem to be accessible or returning the array it is supposed to. Can you check 1) it exists, 2) is readable, 3) ends with return $v?

Thnks for the explanations, just in case im currently using PHP Version 8.1.15RC1, my permissions seems fine

-rwxrwxrwx 1 www-data www-data 642 Jan 17 03:59

and the file itself looks good, will all the values and a final return $v.

As Bob pointed, currently I’m working on 2 plugins and each one has a base class that calls getVersionData so its getting called more than once

@markh Try this code:

echo "First: " . print_r(include_once MODX_CORE_PATH . 
    "docs/", true);

echo "\nSecond: " . print_r(include_once MODX_CORE_PATH . 

I get an array the first time and 1 the second time.

I’m calling it in a test script that initializes MODX before running these lines, so that could make a difference, but I’ve run into this issue a number of times in my Extras that run in MODX.

getVersiondata() only includes the file if $modx->version === null, but I can imagine a situation where the $modx->version gets set to something other than null, but is not an array.

That’s the key point here.

How many times you call $modx->getVersionData() is irrelevant because that should only ever try loading the file once. Hence, include vs include_once is not the problem.

Try changing your code not to include the version file, but rather call $modx->getVersionData() repeatedly. Should have the same output each time.

This is what we need to figure out, because apparently that’s happening here. The only thing I can think of is it not being able of loading the file.

Is something else perhaps loading that file directly rather than use $modx->getVersionData()? In core, only the setup and and the getVersionData load the file.

Just to confirm, calling getVersionData() twice gets the array both times.

Given the 404, it’s quite possible that the version file is not being found. Maybe the host has changed the file and/or folder permissions.

Thanks a lot, Will do some debugging to see where its getting lost, hope I can esaily find where Im breaking it

After doing some debug, found out that in some cases, $this->version is null when it has actually been included, so the call to include_once returns true as expected since the file was already included, next step is to find why and where this is being nulled, my guess is that im using some MODX component the wrong way as part of my migration since I’m not actually setting the value for ‘version’ anywere on my code

public function getVersionData() {
        if ($this->version === null) {
            $this->version= @ include_once MODX_CORE_PATH . "docs/";
        return $this->version;