Object creation method confused - php

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)

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

How does an already-made object be re-created (see example)

I was looking at OOP Basics and saw a code like this (simplified it a bit)
You can see this class and the output
class Test{}
$a = new Test();
$b = new $a;
var_dump($b == $a); // true
What I don't understand is the $b = new $a but $a is already an object, so how/why does this work? If I do vardump $a the output is:
object(Test)#1 (0) {
}
So, how can that variable work with new keyword. I thought we could only use new with a class that is defined already, or with a string that points to a class ex:
$var = 'Test';
new $var; // ok
but in this case, $var is a string, not an another object.
It is a shortcut for creating new object. Before PHP 5.3.0 you have to do this:
$class = get_class($instance);
$newInstance = new $class;
As of PHP 5.3.0 you can do the same thing with this:
$newInstance = new $instance;
Very useful, in my opinion, because it eliminates the need for a temporary variable.
To clarify, this creates new object.
It is not cloning.
In other words, __construct() will be called instead of __clone().

what in PHP is similar to Actionscript 's "is" operator?

As far I know the is operator in ActionScript does the following: (rectify me, if I am wrong)
Tests whether a variable or expression is compatible with a given data type.
Examines the inheritance hierarchy
Can be used to check whether an object is an instance of a particular class or a child (or grandchild, great grandchild, great great grandchild, and so on) of a particular class.
Check whether an object is an instance of a class that implements a particular interface
Now I want to know what in PHP is/are similar to Actionscript 's "is" operator?
In PHP you have construction instanceof:
$a instanceof MyClass
The "is" operator is used for comparing/testing data type membership (type checking). In php, you can used instanceof to check the specific type of the object.
$obj = new A();
if ($obj instanceof A) {
...
}
ActionScript's is operator is equivalent to PHP's instanceof operator
<?php
class Person {}
$p1 = new Person();
$p2 = new Person();
echo ($p1 instanceof $p2)?"True":"False"; //o/p: True bcz both $p1 & $p2 are the instances of same class
echo ($p1 instanceof Person)?"True":"False";//True , checking through class name
interface LoyalCustomer{}
class Customer extends Person implements LoyalCustomer {}
$c1 = new Customer();
echo ($c1 instanceof $p1)?"True":"False";//True
class RegularCustomer extends Customer{}
$rc1 = new RegularCustomer();
echo ($rc1 instanceof $p1)?"True":"False";//True
echo ($rc1 instanceof $c1)?"True":"False";//True
echo ($rc1 instanceof RegularCustomer)?"True":"False";//True
echo ($rc1 instanceof Customer)?"True":"False";//True
echo ($rc1 instanceof Person)?"True":"False";//True
echo ($p1 instanceof LoyalCustomer)?"True":"False";//False
echo ($c1 instanceof LoyalCustomer)?"True":"False";//True
echo ($rc1 instanceof LoyalCustomer)?"True":"False";//True
echo ($p1 instanceof $rc1)?"True":"False";//False
?>

php class names , what's the difference

I am a new in terms of PHP OOP programming, i don't understand when and how the following class names are and when shall i use them :
$a = new Classname();
$a = new Classname;
$a = ClassName::function();
$a = ClassName::getInstance();
Many thanks and sorry for silly question:
These are identical.
$a = new Classname();
$a = new Classname;
You can use them interchangeably when the class constructor does not take, or does not require other parameters.
Example:
class Classname
{
public function __construct($var = null)
{
// ..
}
static public function getInstance()
{
// ..
}
}
In this case you can use $a = new Classname; and $var will take the default value, or $a = new Classname('hello') and $var will be equal to the value passed.
These are both static method calls.
$a = ClassName::function();
$a = ClassName::getInstance();
One calls a method called "function" (which cannot exist - it is a reserved word), the other calls a method named "getInstance". When you use them really depends on what the methods do.
Static methods can be called without creating an object instance.
I.e.
Classname::staticMethod();
versus
$obj = new Classname;
$obj->method;
As for
$a = new Classname();
$a = new Classname;
No difference if __construct() has no arguments to receive.
As for
$a = ClassName::function();
$a = ClassName::getInstance();
this is just normal call of static methods
For:
$a = new Classname();
$a = new Classname;
These are just 2 different ways of saying the same thing: Create a new reference to class "Classname" without any parameters (php is more lenient in regards to if () and parameters must be given or not than many other programming languages).
For:
$a = ClassName::function();
$a = ClassName::getInstance();
These two are static calls of the functions "function()" and "getInstance()", thus $a would be set to the appropriate return value of these function. Static means that you can use the
functions without referening the class itself (thus $b=ClassName(); $a=$b->function() is not needed instead you can just write it as you did above).

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";

Categories