Why does object not reference other object after unserialize? - php

$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.

Related

Why it's happen in php? what is the meaning of the output?

class foo {
public $x;
function __construct() {
$this->x = 5;
}
}
$foo1 = $foo2 = new foo;
$f1 = $f2 = 5;
echo "foo1 = ".$foo1->x;
echo PHP_EOL;
echo "foo2 = ".$foo2->x;
$foo2->x = 50;
echo PHP_EOL;
echo "foo1 = ".$foo1->x;
echo PHP_EOL;
echo "foo2 = ".$foo2->x;
Why it's happen in php? what is the meaning of the output?
I have last time asked without code.
$foo1 = $foo2 = new foo;
Both variables hold the very same object, so setting $foo2->x = 50 also sets it for $foo1 since it's just another name for the same thing.
Unlike arrays, objects in PHP are not copied when they're assigned. So $foo1 and $foo2 both refer to the same foo object. Any changes made to that object will be visible through either variable.
You can either initialize them with separate objects by using new foo twice:
$foo1 = new foo;
$foo2 = new foo;
or you can use clone to copy the object:
$foo1 = clone $foo2 = new foo;
It is because you are assigning $foo1 and $foo2 to the same object. Therefore $foo1->x is the same variable as $foo2->x
What you actually want is creating two different instances of the class foo.
For example:
$foo1 = new foo();
$foo2 = new foo();
If you need further explaination you can take a look at the page about objects and references in the php documentation.

Use string as object name in PHP

I want to use a string as an object name in PHP.
For example:
$name = 'utils';
$obj = new $name();
$utils->test();
So I want to be able to call the object by the name of a string (since I don't know the class names beforehand).
How would I be able to do this?
Try the following:
$name = 'utils';
$$name = new $name();
See Namespaces and dynamic language features ΒΆ for more information
Class Foo {
function bar(){
return 'text';
}
}
$name = 'Foo';
$obj = new $name();
echo $obj->bar();

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

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?.

Instantiate a new class with the value of a string

This might be a very stupid question :P But I found this really interessting:
class SomeClass{
var $var = "this is some text";
function echoVar($name){
echo $this->{$name};
}
}
$class = new SomeClass()
$class->echoVar("var") // will echo "this is some text"
Can I do somethign similar, can I take the value of a string and instantiate a new class with that name? If not, any "almost" solutions?
Thanks
Yes. You can dynamically instantiate classes in PHP. Like this:
$className = 'SomeClass';
$myInstance = new $className();
If your string 'dave' is in $name, you can use it with $$name
$name = 'dave';
$$name = new SomeClass();
$dave->echoVar('var');

Categories