Search code examples
typo3protecteddatahandler

TYPO3 get protected property from DataHandler


Im using a the hook

public function processDatamap_afterAllOperations(DataHandler &$pObj)
{

}

I want to read the protected mmHistoryRecords from the DataHandler Class. How can I do this in my class? My class already extends BackendUtility


Solution

  • You can use reflection to access protected properties or methods. That saves you from XCLASSing.

    public function processDatamap_afterAllOperations(\TYPO3\CMS\Core\DataHandling\DataHandler $pObj)
    {
        try {
            $prop = new \ReflectionProperty(
                \TYPO3\CMS\Core\DataHandling\DataHandler::class, 
                'mmHistoryRecords'
            );
            $prop->setAccessible(true);
    
            $mmHistoryRecords = $prop->getValue($pObj);
        } catch (\ReflectionException $e) {
    
        }
    }
    

    You can use it on methods and properties. So in PHP nothing is ever "private"... You need to judge yourself if you should do it, but it's always a possiblity.