Is it possible to dynamically add members to PHP objects? - php

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

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.

php - How to access objects using string?

I know accessing object's properties dynamically using string e.g.
$obj->{$string};
But what about objects themselves?
Like I have string
$obj = '$model->property';
How to use this?
For example in if statement, to have something like
if($model->property) but by using this string?
Tried if({$obj}), if(${$obj})... nothing works.
I don't know if it even possible, but maybe?
I've set up a small test case...
class A {
public $b = 5;
}
$test = new A();
$var = "test";
echo ${$var}->b;
I think this last line is what your after.
Update:
If you want the object and the property, then the nearest I could get is to use...
class A {
public $b = 5;
}
$test = new A();
$var = "test->b";
list($var, $property) = explode("->", $var);
echo ${$var}->$property;

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().

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.

PHP: Alternative to variables passed by reference?

In the minutes php6 developer meeting, i came across a point where it said that call-time-pass-by-reference is no longer there in PHP6. For example, following both are incorrect for PHP6:
<?php
$foo =& new StdClass();
?>
<?php
function &foo()
{
return new StdClass();
}
$f = foo();
?>
If we can't use something like this in PHP6:
$foo =& new StdClass();
What is the alternative to that, is there any way to mimic that?
EDIT:
Ans what about variables in PHP6, can we do that for variables eg:
$this->data =& $_SESSION;
You don't pass parameters by reference, but you still can/must declare your function/method as receiving parameters by reference.
i.e., you don't do this (passing parameters by reference) :
my_function(& $my_var);
function my_function($a) {
// ...
}
But you can do this (declaring the function as receiving parameters by reference) :
my_function($my_var);
function my_function(& $a) {
// ...
}
And... The code examples you gave are not related to call-time-pass-by-reference, but are related to return-by-reference.
For that second thing, what if you just remove the & ? The instance of the object that's been created inside the function will be returned, and you'll still be able to work with it, won't you ?
Objects are always passed by reference since PHP5, so this:
$foo =& new StdClass();
is the same as:
$foo = new StdClass();
.
Ans what about variables in PHP6, can we do that for variables eg:
$this->data =& $_SESSION;
I see no reason why not -- PHP will not be removing references, as they are far too useful. There is no other way to create a reference to a variable.
Call-time pass-by-reference has been deprecated for a while. It just means that instead of doing this:
function foo($a) { return ++$a; }
foo(&$my_a);
You would do this:
function foo(&$a) { return ++$a; }
foo($my_a);
This leads to a much cleaner and easier-to-understand programming style, and it also ensures that variables are always referenced when you expect, and not when you don't.
Dealing with objects is a special case -- they are always passed by reference, and the only way to simulate pass-by-value is to use clone:
$a = new StdClass();
$a->foo = 'bar';
$b = $a;
$a->foo = 'qux';
// $b->foo is 'qux' too
$a = new StdClass();
$a->foo = 'bar';
$b = clone $a;
$a->foo = 'qux';
// $a->foo is 'qux' but $b->foo is 'bar'
Hope that helps!

Categories