Or, does my variable hold the object itself?
When I say for example:
$obj = new ClassOne();
is the $obj a pointer to the object created in the memory? Does it hold only the memory address to the object? Or does it hold the object itself?
For example when I say,
$obj = new SomeOtherClass();
Will ClassOne object be garbage collected like in JAVA, or will it cause a memory-leak like in C++?
Briefly, the object models in C++ and Java are a bit different:
C++ has unconstrained variables: Every object type can occur as the type of an object that is a variable. In other words, variables can be objects of any type. (But not all variables are objects (e.g. references)!) Moreover, all variables are scoped, and thus the lifetime of all objects-that-are-variables is also scoped automatically. Only dynamically-allocated objects can never be variables, and they can only be handled via pointers and references.
In Java, if we ignore the primitive types, variables are never objects, and objects can never be variables. All objects are always "magically somewhere else" (e.g. "the GC heap"), and you can only ever handle them through pointer-like handles. In Java, a variable of type
Tis always a reference to the actual object of typeT, which lives somewhere else. Variables are also scoped, like in C++, but the lifetime of all Java objects is indeterminate, and only guaranteed to extend beyond the lifetime of all references to a given object.(The situation is different for built-in, "value"-type types like
int, which can occur as the type of variables, and in fact cannot be allocated dynamically.)I think PHP is similar to Java in that regard.