I am using php5.
How could I determine if an instantiated object is the same or a clone.
Example:
Singleton pattern will create/return THE SAME instance of an object.
Factory pattern will create/return ANOTHER instance of an object.
How could I find out that this is trully so -- without testing with some internal(object) properties etc....and see if they change.....
Is there a function for this.
Like for example there is is_a to check if an object is from a given class.
Thanks.
Determine if object instance is same reference or clone.
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
PHP Manual wrote: 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.
On the other hand, when using the identity operator (===), object variables are identical if and only if they refer to the same instance of the same class.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
So then:Weirdan wrote:PHP Manual wrote: 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.
On the other hand, when using the identity operator (===), object variables are identical if and only if they refer to the same instance of the same class.
Code: Select all
class myClass
{
//
}
$foo = new myClass();
$bar = &$foo;
if ($foo === $bar) echo 'Yes';
else echo 'No';Code: Select all
$foo = new myClass();
$bar = new myClass();
if ($foo === $bar) echo 'Yes';
else echo 'No';