tags:

views:

57

answers:

3

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 if i am wrong)

Is this the same with PHP?

A: 

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

fabrik
+1  A: 

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.

Sarfraz
=) wow thank you for that funciton =)
denniss
@denniss: You are welcome..
Sarfraz
+2  A: 

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 container is created, this container stores, inter alia, the id of the object;
  • the variable $var is associated with this created zval container.
Artefacto