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

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.

Related

Access to object property php [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I'm trying to acces to range but is tnot working
$json = '{"range":[[1,2,20],[3,4.5]]}';
var_dump(json_decode($json->'range'));//doesn't work
var_dump(json_decode($json['range']));//doesn't work
Whay is the way to acces to range?
You need to json_decode() your JSON string first to turn it into an object. After that you can access the object properties with the -> operator.
So the correct way is this
var_dump(json_decode($json)->range));
But it is more readable if you split it into multiple statements:
$decoded = json_decode($json);
var_dump($decoded->range);
you must use this one.
json_decode($json)->range;

How to read value from SimpleXMLElement by using PHP 7.0 [duplicate]

This question already has answers here:
SimpleXML Reading node with a hyphenated name
(2 answers)
Closed 4 years ago.
I have an XML with product. I have a problem with read values witch has a "-" in name because this method dosent work:
$productHurtoID = $product->kod-kreskowy->__toString();
Below You have $product variable:
SimpleXMLElement object {
nazwa => SimpleXMLElement object
kod-katalogowy => (string) K0428
kod-kreskowy => (string) 0027084373370
}
Thanks for help. Kind regards
You could access your dynamically created properties via
$productHurtoID = $product->{'kod-kreskowy'}->__toString();
A different approach would be to pre-process your XML input and replace hyphens with camelCase. But depending on your use-case, this might not be possible.
Regards
You can use following code for getting values with - (hyphen) in key.
$productHurtoID = $product->nazwa->{'kod-kreskowy'}->__toString();

How to convert output of date("Ymd") into a number in PHP? [duplicate]

This question already has answers here:
How do I convert a string to a number in PHP?
(35 answers)
Closed 5 years ago.
I want to have the desired output as such 20170613 which is an integer.
I know using strtotime() I can get an UNIX timestamp as an integer, but I don't want that. date("Ymd") however returns a string.
I can't seem to figure a way to convert this to an integer.
Edit #1: Here is what I am attempting:
$x = (int)date("Ymd");
echo $x;
The result however does not show up in the browser. Infact in the developer's tools, it shows internal server error.
The term to Google is "type cast". That leads you to the PHP type juggling docs on integer casting.
Taking that as a reference point, the canonical way to go about it is:
$int = (int)date('Ymd');
For completeness, you could also use the equivalent full form:
$int = (integer)date('Ymd');
Or the functional:
$int = intval(date('Ymd'));

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

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.

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?

Categories