Unfortunately I cannot provide any code examples, however I will try and create an example.
My question is about Objects and memory allocation in PHP.
If I have an object, lets say:
$object = new Class();
Then I do something like
$object2 = $object;
What is this actualy doing? I know there is a clone function, but thats not what I’m asking about, I’m concerned about whether this is creating another identical object, or if its just assigning a reference to $object.
I strongly understand this to mean that it just creates a reference, but in some case usages of mine, I find that I get another $object created, and I can’t understand why.
If you use the magic method
__invoke, you can call an object similar to a function, and it will call that magic method.That means that
$object2is equal to whatever that function returns.Basically, you are calling a function, but using a variable as it’s name. So:
In this case, you are just calling an object instead.
So, in reference to your question, this is actually not ‘cloning’ at all, unless the
__invoke()function looks like this:In which case, it would be a reference to the same class.