Fix calling nested shutdown functions [closes #2981]

PHP allows nesting shutdown functions and ensures, that all will be called in the order they were registered.