How to Avoid PHP Object Nesting/Creation Limit?

后端 未结 1 621
盖世英雄少女心
盖世英雄少女心 2021-01-23 07:38

I\'ve got a handmade ORM in PHP that seems to be bumping up against an object limit and causing php to crash. Here\'s a simple script that will cause crashes:

&l         


        
相关标签:
1条回答
  • 2021-01-23 08:19

    Interestingly, in my environment, it appears that the segfault occurs when it comes time to deconstruct the objects -- code placed after the loop runs fine. It's only when PHP starts to shutdown that the segfault occurs.

    You could file a bug, but you may find that PHP's maintainers won't go out of their way to support this sort of thing. I've seen at least one bug report about a memory leak in which the official response was essentially "Wontfix: memory is released after the page is rendered, so this doesn't really matter" -- effectively implying that uses outside of the simple case of rapidly rendering a webpage and terminating aren't really supported.

    After 5 years of full-time PHP development, I've arrived at a simple rule: if it crashes PHP, don't do it. PHP has its limitations, and you'll find yourself most successful if you don't push those limits.

    That means things like avoiding create_function() in PHP <=5.2 (it leaks memory like crazy). You can try to use create_function() to use PHP as if it were a functional language. It's not, and you'll find it fails miserably if you try to use it as such.

    So if PHP chokes on nesting objects 40000 levels deep... don't nest objects 40000 levels deep. One possible alternative is using arrays instead of objects -- but that still sounds pretty heinous.

    0 讨论(0)
提交回复
热议问题