Destroying php object references - php

I will try to be succinct on this:
I have this:
Example1:
$obj1 = new SimpleObj();
$obj2 = $obj1;
$obj2 = null;
Example2:
$obj1 = new SimpleObj();
$obj2 = clone $obj1;
$obj2 = null;
Example3:
$obj1 = new SimpleObj();
$obj2 =& $obj1;
$obj2 = null;
I did a lot of research on this, but i'm not assured yet.
Which one will actually destroy the object and free memory?
Thank in advance!

Related

Creating an instance from an instance of a class in PHP?

This question comes form the following link
http://php.net/manual/en/language.oop5.basic.php
Specifically, Example #5 Creating new objects
$obj1 = new Test();
$obj2 = new $obj1;
var_dump($obj1 !== $obj2); //bool(true)
I can't figure out why it is okay or logical to use the new operator on $obj1 to create $obj2 when $obj1 is clearly just an object, not a class?
I have been trying to find reference on this topic, but could not find one.
Thanks
I have no idea why they added a possibility to create a new object with an instance of the class but the instance is used as a class in this case. You even can pass constructor arguments. Try this code:
class Waat {
public $message;
public function __construct($message )
{
$this->message = $message;
}
}
$w1 = new Waat('hello');
$w2 = new $w1('world');
echo $w1->message . ' ' . $w2->message;
It outputs:
hello world

Object creation method confused

I have read the manual for this point but I cannot understand the logic behind this.
http://www.php.net/manual/en/language.oop5.basic.php
$obj1 = new Test();
$obj2 = new $obj1;
var_dump($obj1 === $obj2); //bool(false)
How this happen even though,
var_dump($obj1); //object(Test)#1 (0) { }
var_dump($obj2); //object(Test)#2 (0) { }
I expect the bool(true) but it is "bool(false)".
According to the manual:-
when using the identity operator (===), object variables are identical if and only if they refer to the same instance of the same class
You have two separate instances of the same class, so === evaluates to false.
So, $obj1 === $obj2 evaluates to false, but $obj1 == $obj2 would evaluate to true as
When using the comparison operator (==), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values, and are instances of the same class.
It may be that you are unaware that you have created a new instance of Test by doing $obj2 = new $obj1;. Change this to $obj2 = $obj1; and you will find that $obj1 === $obj2 evaluates to true as you now have two variables refering to the same instance of Test.
Objects are only equivelent if they refer to the same instance.
$obj1 = new Test();
$obj2 = new Test();
var_dump($obj1 === $obj2); // bool(false)
But
$obj1 = new Test();
$obj2 = &$obj1;
var_dump($obj1 === $obj2); // bool(true)

Is it possible to dynamically add members to PHP objects?

Is it possible to add to PHP objects on the fly? Say I have this code:
$foo = stdObject();
$foo->bar = 1337;
Is this valid PHP?
That's technically not valid code. Try something like:
$foo = new stdClass();
$foo->bar = 1337;
var_dump($foo);
http://php.net/manual/en/language.types.object.php
It is valid as long as you use valid class eg stdClass instead of stdObject:
$foo = new stdClass();
$foo->bar = 1337;
echo $foo->bar; // outputs 1337
You had these problems:
Using stdObject instead of stdClass
Not instantiating your object using new keyword
More Info:
http://php.net/manual/en/language.types.object.php
You're close.
$foo = stdObject();
This needs to be:
$foo = new stdClass();
Then it will work.
Yes it is. The only problem in your code is that it's missing a new before calling stdClass, and you're using stdObject, but you mean stdClass
<?php
class A {
public $foo = 1;
}
$a = new A;
$b = $a; // $a and $b are copies of the same identifier
// ($a) = ($b) = <id>
$b->newProp = 2;
echo $a->newProp."\n";

Why does object not reference other object after unserialize?

$obj = new classname();
$obj1 = $obj;
$obj->var = 'something';
$obj = unserialize(serialize($obj));
$obj->var= 'something new';
Now obj1->var = 'something' and $obj->var = 'something new'.
Why does $obj,$obj1 not reference to the same thing?
The function unserialize acts like an alternative constructor. It will always create a new object.
When you did $obj = unserialize(serialize($obj));, you created a new object.

How to specify class dynamically in PHP?

So normal class methods and object creation goes like this...
$obj = new Class();
$obj2 = Class::someMethod();
Can I instantiate "class" dynamically? How? I want to do something like...
$class = "Class";
$obj = new $class?;
$obj2 = $class?::someMethod();
$class = "Class";
$obj = new $class; // works
$obj2 = $class::someMethod(); // works as of PHP 5.3
// if you don't have PHP 5.3 and want to use the ladder:
$obj2 = call_user_func(array($class, 'someMethod'));
That should work without the ? in $obj = new $class?.

Categories