开发者

So what really happens when we use 'new' in PHP

开发者 https://www.devze.com 2023-01-09 03:05 出处:网络
So what really happens when someone say \'new\' in PHP I believe in C/Java, when new is called, memory is allocated for each instance variables that are needed for an object? (correct me i开发者_运维

So what really happens when someone say 'new' in PHP

I believe in C/Java, when new is called, memory is allocated for each instance variables that are needed for an object? (correct me i开发者_运维百科f i am wrong)

Is this the same with PHP?


When you use $var = new Class

  • a new object is created (memory allocated and initialized);
  • its constructor, if any, is called;
  • the object is put into a list of objects and given a unique id;
  • a new zval container1 is created, this container stores, inter alia, the id of the object;
  • the variable $var is associated with this created zval container.


1 Definition of what's a zval container.


The easiest way would be to check it for yourself using memory_get_usage()

echo memory_get_usage();
$obj1 = new obj1;
$obj2 = new obj2;
$obj3 = new obj3;
echo memory_get_usage();

Same is the case with PHP.


When you say new in PHP, PHP assumes you'd like to call a new Class: PHP Classes Basics.

0

精彩评论

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