开发者

Is it possible to set a memory limit specifically to a variable?

Is it possible to set a memory limit specifically to a variable ? (This limit would be of course useless if greater than the memory_limit in php.ini.)

I would like 开发者_运维知识库$foo to be always less big than 1mb for example, but don't care about other variables's size.


No, it is not possible to impose a size limit on variables in PHP.

Depending on what you want to do, you could build a class with a setter function that checks the size before assigning a value to a variable of the class.


What is $foo in your situation? If it is a string, you can do it with chars limit. If it is a number, you can check number size.


Based on the idea of checking the number of characters of a string : I can have the string representation of $foo with, for example, print_r($foo,true) and check the number of char.

if(strlen(print_r($foo,true))<10000){
  /*do something*/
};

(although this solution isn't to use as time and power consuming. And it's a really rough way to check the memory space needed).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜