Information Required ?
avatar horvathcsabazoltan
horvathcsabazoltan
30 Jul 2021

Steps to reproduce the issue

  1. Create a registry with an object item in it
  2. Merge it into another registry
  3. The resulting registry will return the item as array

Expected result

Preserve item type (including class)

Actual result

The original item returned as array

System information (as much as possible)

Joomla 3.9.26

Additional comments

avatar horvathcsabazoltan horvathcsabazoltan - open - 30 Jul 2021
avatar joomla-cms-bot joomla-cms-bot - change - 30 Jul 2021
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 30 Jul 2021
avatar ReLater
ReLater - comment - 30 Jul 2021

Can't confirm when using recursive = true in merge():

defined('_JEXEC') or die;
use Joomla\Registry\Registry;

$obj1 = new stdClass;
$obj1->a = 'a';
$obj1->b = 'b';
$obj1->c = 'c';

$arr1 = ['d', 'e', 'f'];

$registry1 = new Registry;
$registry2 = new Registry;

$registry1->set('val1', 1);
$registry1->set('val2', 2);
$registry1->set('obj1', $obj1);
$registry1->set('arr1', $arr1);
$registry2->set('val3', 3);

$registry2->merge($registry1, true);

echo '  <pre>' . print_r($registry2, true) . '</pre>';exit;

Output:

Joomla\Registry\Registry Object
(
    [data:protected] => stdClass Object
        (
            [val3] => 3
            [val1] => 1
            [val2] => 2
            [obj1] => stdClass Object
                (
                    [a] => a
                    [b] => b
                    [c] => c
                )

            [arr1] => Array
                (
                    [0] => d
                    [1] => e
                    [2] => f
                )

        )

    [initialized:protected] => 1
    [separator] => .
)
avatar richard67
richard67 - comment - 30 Jul 2021

@horvathcsabazoltan Can you check and report back if it works for you when doing as advised in the previous comment? Thanks in advance.

avatar richard67 richard67 - change - 30 Jul 2021
Labels Added: Information Required
avatar richard67 richard67 - labeled - 30 Jul 2021
avatar horvathcsabazoltan
horvathcsabazoltan - comment - 30 Jul 2021

It's almost perfect. But all classes are converted to stdClass.

defined('_JEXEC') or die;
use Joomla\Registry\Registry;

class FooBar {
    public $property1 = 'property-1-value';
    public $property2 = 'property-2-value';
}

$registry = new Registry();
$registry->set('associative-array-item', ['key1' => 'value1', 'key2' => 'value2']);
$registry->set('object-item', (object)['property1' => 'value1', 'property2' => 'value2']);
$registry->set('class-item', new FooBar);

echo '<pre>Before merge<br>';
print_r($registry);

$merge1 = new Registry(['merge' => '$recursive = false']);
$merge1->merge($registry);
$merge2 = new Registry(['merge' => '$recursive = true']);
$merge2->merge($registry, true);

echo 'Merge results:<br>';
print_r($merge1);
print_r($merge2);

echo '</pre>';

Output:

Before merge
Joomla\Registry\Registry Object
(
    [data:protected] => stdClass Object
        (
            [associative-array-item] => Array
                (
                    [key1] => value1
                    [key2] => value2
                )

            [object-item] => stdClass Object
                (
                    [property1] => value1
                    [property2] => value2
                )

            [class-item] => FooBar Object
                (
                    [property1] => property-1-value
                    [property2] => property-2-value
                )

        )

    [initialized:protected] => 
    [separator] => .
)
Merge results:
Joomla\Registry\Registry Object
(
    [data:protected] => stdClass Object
        (
            [merge] => $recursive = false
            [associative-array-item] => Array
                (
                    [key1] => value1
                    [key2] => value2
                )

            [object-item] => Array
                (
                    [property1] => value1
                    [property2] => value2
                )

            [class-item] => Array
                (
                    [property1] => property-1-value
                    [property2] => property-2-value
                )

        )

    [initialized:protected] => 1
    [separator] => .
)
Joomla\Registry\Registry Object
(
    [data:protected] => stdClass Object
        (
            [merge] => $recursive = true
            [associative-array-item] => stdClass Object
                (
                    [key1] => value1
                    [key2] => value2
                )

            [object-item] => stdClass Object
                (
                    [property1] => value1
                    [property2] => value2
                )

            [class-item] => stdClass Object
                (
                    [property1] => property-1-value
                    [property2] => property-2-value
                )

        )

    [initialized:protected] => 1
    [separator] => .
)
avatar horvathcsabazoltan
horvathcsabazoltan - comment - 31 Jul 2021

Another strange thing: associative arrays are converted to stdClass if $recursive = true

defined('_JEXEC') or die;
use Joomla\Registry\Registry;

class FooBar {
    public $property1 = 'property-1-value';
    public $property2 = 'property-2-value';
    public $arrayprop = ['key1' => 'value1', 'key2' => 'value2'];
}

$registry = new Registry();
$registry->set('associative-array-item', ['key1' => 'value1', 'key2' => 'value2']);
$registry->set('class-item', new FooBar);

echo '<pre>Before merge<br>';
print_r($registry);

$merge2 = new Registry(['merge' => '$recursive = true']);
$merge2->merge($registry, true);

echo 'Merge results:<br>';
print_r($merge2);
echo '</pre>';

Output:

Before merge
Joomla\Registry\Registry Object
(
    [data:protected] => stdClass Object
        (
            [associative-array-item] => Array
                (
                    [key1] => value1
                    [key2] => value2
                )

            [class-item] => FooBar Object
                (
                    [property1] => property-1-value
                    [property2] => property-2-value
                    [arrayprop] => Array
                        (
                            [key1] => value1
                            [key2] => value2
                        )

                )

        )

    [initialized:protected] => 
    [separator] => .
)
Merge results:
Joomla\Registry\Registry Object
(
    [data:protected] => stdClass Object
        (
            [merge] => $recursive = true
            [associative-array-item] => stdClass Object
                (
                    [key1] => value1
                    [key2] => value2
                )

            [class-item] => stdClass Object
                (
                    [property1] => property-1-value
                    [property2] => property-2-value
                    [arrayprop] => stdClass Object
                        (
                            [key1] => value1
                            [key2] => value2
                        )

                )

        )

    [initialized:protected] => 1
    [separator] => .
)
avatar ReLater
ReLater - comment - 31 Jul 2021

All I can say is: "That is by design." if I inspect the Registry class and and merge(), bindData() .

I think the right place for the issue/discussion is https://github.com/joomla-framework/registry

Joomla 3 uses version 1.6 at the moment https://github.com/joomla-framework/registry/blob/1.6.3/src/Registry.php . Joomla 4 the current 2 beta https://github.com/joomla-framework/registry/blob/2.0.0-beta/src/Registry.php . But there I don't see a change concerning this issue here.

avatar horvathcsabazoltan
horvathcsabazoltan - comment - 31 Jul 2021

@ReLater: Thank you, I submitted a new issue where you said: joomla-framework/registry#57

avatar alikon alikon - change - 2 Aug 2021
Status New Closed
Closed_Date 0000-00-00 00:00:00 2021-08-02 05:25:29
Closed_By alikon
avatar alikon alikon - close - 2 Aug 2021
avatar alikon
alikon - comment - 2 Aug 2021

closing here then

Add a Comment

Login with GitHub to post a comment