prevent PHP errors and enhance logging

fixes #10094

@butonic @schiesbn  please review and test - THX
line 165 returns null in case of error as well - we better change the the doc comment. Thanks for noticing