Preserve item type (including class)
The original item returned as array
Joomla 3.9.26
Labels |
Added:
?
|
@horvathcsabazoltan Can you check and report back if it works for you when doing as advised in the previous comment? Thanks in advance.
Labels |
Added:
Information Required
|
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] => .
)
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] => .
)
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.
@ReLater: Thank you, I submitted a new issue where you said: joomla-framework/registry#57
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2021-08-02 05:25:29 |
Closed_By | ⇒ | alikon |
closing here then
Can't confirm when using recursive = true in merge():
Output: