denniss
denniss

Reputation: 17589

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

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?

Upvotes: 5

Views: 4557

Answers (3)

Artefacto
Artefacto

Reputation: 97825

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.

Upvotes: 6

Sarfraz
Sarfraz

Reputation: 382696

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.

Upvotes: 2

fabrik
fabrik

Reputation: 14365

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

Upvotes: 0

Related Questions