How can magische Konstanten like __CLASS__ and __TRAIT__ be accessed within a code string evaluated by eval() in PHP?

To access magic constants like __CLASS__ and __TRAIT__ within a code string evaluated by eval() in PHP, you can use the get_defined_constants() function to retrieve all the defined constants and then pass them as a parameter to the eval() function. This way, the magic constants will be available for use within the evaluated code string.

// Get all defined constants
$constants = get_defined_constants(true);

// Pass the constants as a parameter to eval()
eval('
    // Your code string here
    echo "Class name: " . $constants["__CLASS__"] . "\n";
    echo "Trait name: " . $constants["__TRAIT__"] . "\n";
');