Problem
Because DependencySerializationTrait uses get_object_vars()
to collect serializable service properties, but that function cannot access private properties. Because of that, the code below fails, because $entityTypeManage
r becomes null when deserialization happens.
<?php
final class MyForm extends \Drupal\Core\Form\FormBase {
private $entityTypeManager;
public function __construct(\Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
public static function create(\Symfony\Component\DependencyInjection\ContainerInterface $container) {
return new static(
$container->get('entity_type.manager')
);
}
}
This seems to be a known limitation for 2 years or so: https://www.drupal.org/project/drupal/issues/2727011#comment-12619886
Proposed solution
Use Reflection or Clousers instead of get_object_vars().
TODOs
Monitor and measure performance changes: https://www.lambda-out-loud.com/posts/accessing-private-properties-php/