How to pass a PHP object NOT by reference [duplicate] - php

This question already has answers here:
How do I create a copy of an object in PHP?
(9 answers)
Closed 6 years ago.
In PHP objects are automatically passed by reference:
$obj1 = new stdClass();
$obj1->foo = 'bar';
$obj2 = $obj1;
$obj2->foo = 'OOF';
var_dump($obj1->foo); // OOF
Is there an elegant way to copy that variable and NOT refer to the original variable? I want to store a copy of an object and then modify it without effecting the original. Thanks.

You can clone the object:
$obj2 = clone $obj1;
Note that $obj2 will be a shallow copy of $obj1. As stated in the PHP manual:
When an object is cloned, PHP 5 will perform a shallow copy of all of
the object's properties. Any properties that are references to other
variables will remain references.
You can override the __clone() method to manually clone any subobjects if you wish.

Related

What does "->" do in PHP? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 7 years ago.
I am studying how to connect database while learning PHP. Just a quick question. Does anyone can tell me what does "->" sign do in PHP? I cannot understand the functionality of this sign so that I have no idea how to edit the code. Thank whoever answer this.
-> Sign used in objects, to access it's property and methods.
class example{
public $prop1 = 'Hello World';
public function sayHello(){
echo $this->prop1;
}
}
$example = new example();
$example->sayHello();
Ref: Classes and Objects in PHP
-> Is Used to refer the Classes And Objects for more information check here.
You haven't posted any code, so I'm not 100% sure where you saw this, but I'm almost certain you are referring to something like this:
$foo = new Foo();
echo $foo->bar;
In this example, -> is used to access a property of an object, $foo. It can also be used to access a method, as in $foo->baz();.
For real quick and dirty one-liner anonymous objects, just cast an associative array:
<?php
$obj = (object) array('foo' => 'bar', 'property' => 'value');
echo $obj->foo; // prints 'bar'
echo $obj->property; // prints 'value'
?>
... no need to create a new class or function to accomplish it.

PHP - Why is DateTime object copied by reference in my code? [duplicate]

This question already has answers here:
Object copy versus clone in PHP
(4 answers)
Closed 8 years ago.
Why in this code my DateTime object was copied by reference it seems?
Here's my code:
<?php
date_default_timezone_set('UTC');
$dt1 = new \DateTime('2015-03-15');
$dt2 = $dt1;
$dt2 = $dt2->modify('-1 year');
echo $dt1->format('c') . PHP_EOL;
echo $dt2->format('c');
?>
I was expecting:
2015-03-15T00:00:00+00:00
2014-03-15T00:00:00+00:00
But I got this:
2014-03-15T00:00:00+00:00
2014-03-15T00:00:00+00:00
It's because of this line
$dt2 = $dt1;
Variables get copied, objects get referenced.
See this for an answer with examples - https://stackoverflow.com/a/6257203/1234502
You should be able to fix this with clone
Consider the following text from PHP's Objects and references page:
As of PHP 5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object.
Basically, your $dt2 = $dt1; is simply copying the object reference and not its contents; see the response by #lolka_bolka for the appropriate means by which to perform this task.

PHP dynamic instanciating [duplicate]

This question already has answers here:
In PHP, can you instantiate an object and call a method on the same line?
(9 answers)
Closed 8 years ago.
I've an object which returns a string and I would like instanciate another object with the string returned, why in PHP I can't instanciate this way ?
For Example:
// getController() returns a string name controller
$c = new $this->router->getController() ;
// I have to do this way:
$controller = $this->router->getController() ;
$c = new $controller() ;
Thank you for your help.
It's a syntax thing. I think I remember seeing an RFC to allow more dynamic class instantiation, but assigning to a variable first gets the job done, no?

What's the difference in new stdClass() and new stdClass? [duplicate]

This question already has answers here:
Instantiate a class with or without parentheses? [duplicate]
(3 answers)
Closed 9 years ago.
I'm learning the advance concepts of php. What does new stdClass do? I know what new stdClass(); does.
For eg) <?php new stdClass(); ? > creates a new object. Does <?php new stdClass; ? > do the same thing? Notice there's no parentheses. Does that make a difference? I can't find documentation on it for php manual.
There is no difference. PHP lets you omit the parentheses if you're not passing arguments to the constructor.

create class directly after a property field? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
declare property as object?
in java you can create an object directly after the property field like this:
but it seems not working for php:
class Test {
public $object = new Object();
}
you have to create it in the __construct() and assign it to the property?
thanks
From php.net
This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
So no, you cant initialize it to an object. You'll have to do it in the constructor like you said

Categories