PHP arrays work differently for objects vs values [duplicate] - php

This question already has answers here:
How do I create a copy of an object in PHP?
(9 answers)
Closed 3 years ago.
Here's my code:
$words = array();
$word = "this";
$words[] = $word;
$word = "that";
$words[] = $word;
print_r($words);
class word{
private $text;
public function __construct($word){
$this->text=$word;
}
public function setWord($word){
$this->text=$word;
}
}
$class_words = array();
$word = new word("this");
$class_words[] = $word;
$word->setWord("that");
$class_words[] = $word;
print_r($class_words);
exit;
Here's the output:
Array
(
[0] => this
[1] => that
)
Array
(
[0] => word Object
(
[text:word:private] => that
)
[1] => word Object
(
[text:word:private] => that
)
)
I expected the second output to match the first in that the array should store 'this' and 'that'. It seems array_name[] = <item> makes a copy to the item when it's an array of values but not so when it's an array of objects. How do I make it copy the object to the array instead copying a reference to the object? Do I need to create a new object each time I need to add an object to the array?

If you want to copy the value of the object into the array you need to write a "getter" for the value e.g.
class word{
private $text;
public function __construct($word){
$this->text=$word;
}
public function setWord($word){
$this->text=$word;
}
public function getWord() {
return $this->text;
}
}
$class_words = array();
$word = new word("this");
$class_words[] = $word->getWord();
$word->setWord("that");
$class_words[] = $word->getWord();
print_r($class_words);
Output:
Array
(
[0] => this
[1] => that
)
Demo on 3v4l.org

$x = new X(); stores a reference to an object into $x. A subsequent $y = $x; copies the reference, not the object, so $x and $y both refer to the same object.
PHP has rather complex semantics regarding references.

Objects are always references; all your uses of $word refer to the same object and the same data structure. You need to do:
$class_words=[new word('this'),new word('that')];

Both elements of your array hold the same object. So whenever you make a change in that object, all references to that object will show that change-- they're all referring to the same object in its current state.
As mentioned previously, if you want different values, you need to either instantiate new objects, or get the property value via a getter() that returns a simple value (string, integer, boolean, etc), not the object itself.
As a side note, you can leverage the referential nature of objects to chain methods ($Obj->method1()->method2()->method3()) by having a method return a reference to the object, i.e., return $this;

Related

Accumulate strings and array values into an array as a class property via method

I have a class with method add() that accepts strings and arrays. I need to have an array with all users, but I cannot seem to get it. All I get is multiple arrays with all users. How could I merge those arrays into one?
class Users {
function add($stringOrArray) {
$arr = array();
if(is_array($stringOrArray)) {
$arr = $stringOrArray;
} else if(is_string($stringOrArray)) {
$arr[] = $stringOrArray;
} else {
echo('errrrror');
}
print_r($arr);
}
When I use this test:
public function testOne() {
$users = new Users();
$users->add('Terrell Irving');
$users->add('Magdalen Sara Tanner');
$users->add('Chad Niles');
$users->add(['Mervin Spearing', 'Dean Willoughby', 'David Prescott']);
This is what I get, multiple arrays but I need one array.
Array
(
[0] => Terrell Irving
)
Array
(
[0] => Magdalen Sara Tanner
)
Array
(
[0] => Chad Niles
)
Array
(
[0] => Mervin Spearing
[1] => Dean Willoughby
[2] => David Prescott
)
You can cut a lot of unnecessary bloat from your method.
You can cast ALL incoming data to array type explicitly. This will convert a string into an array containing a single element. If the variable is already an array, nothing will change about the value.
Use the spread operator (...) to perform a variadic push into the class property.
Code: (Demo)
class Users
{
public $listOfUsers = [];
function add($stringOrArray): void
{
array_push($this->listOfUsers, ...(array)$stringOrArray);
}
}
$users = new Users;
$users->add('Terrell Irving');
$users->add(['Magdalen Sara Tanner', 'Chad Niles']);
$users->add(['Mervin Spearing']);
var_export($users->listOfUsers);
Output:
array (
0 => 'Terrell Irving',
1 => 'Magdalen Sara Tanner',
2 => 'Chad Niles',
3 => 'Mervin Spearing',
)
All you need is to store the added users in a class property, for example $listOfUsers.
If adding the array you use the array_merge() function otherwise just add new user at the end of indexed array.
<?php
class Users {
// here will be all the users stored
public $listOfUsers = array();
function add($stringOrArray) {
//$arr = array();
if(is_array($stringOrArray)) {
// merge two arrays - could create duplicate records
$this->listOfUsers = array_merge($this->listOfUsers, $stringOrArray);
} else if(is_string($stringOrArray)) {
// simply add new item into the array
$this->listOfUsers[] = $stringOrArray;
} else {
echo('errrrror');
}
print_r($this->listOfUsers);
}
}
In your example you are storing the data locally within the method add() and it is not kept for future usage. This behavior is corrected using the class property $listOfUsers that can be accesed using $this->listOfUsers within the class object and if needed outside of the class.

Object assignment is creating a reference in PHP

I have a weird behavior when assigning an existant object to an array.
In the example below, I have a class that contains one property. I create a first array with 3 instances (let's call them 2-4-6) and then a second array with 2 other instances using one object of the first array (instance 4). While modifying the values of the objects in second array to create 2 new instances (ie instances 3-5), the instance 4 is also modified. In consequence, at the first array query I get the right values (2-4-6) but after creating the second array I get the modified value (2-5-6). I would expect that the assignment operator copied the object, but instead it creates a reference to the instance. In the example I can get rid of this issue by explicitly calling clone, but in a larger scale, this isn't work (wrong code optimization?). Any clue (or good practice) of how to avoid this issue?
Thanks!
<?php
class TestBase
{
private int $m_test = 0;
public function SetTest(int $v)
{
$this->m_test = $v;
}
public function GetTest() : int
{
return $this->m_test;
}
}
function getNewList(TestBase $ref) : array
{
$newlist = [3 => new TestBase(), 5 => $ref];
$newlist[3]->SetTest(3);
$newlist[5]->SetTest(5);
return $newlist;
}
$listOfTest = [2 => new TestBase(), 4 => new TestBase(), 6 => new TestBase()];
$listOfTest[2]->SetTest(2);
$listOfTest[4]->SetTest(4);
$listOfTest[6]->SetTest(6);
foreach ($listOfTest as $test)
{
echo $test->GetTest().'<br>';
}
// 2
// 4
// 6
//$ref = clone $listOfTest[4];
$ref = $listOfTest[4];
$newList = getNewList($ref);
foreach ($listOfTest as $test)
{
echo $test->GetTest().'<br>';
}
// 2
// 5
// 6
?>
The problem is in the way the PHP uses arrays. Array assignment by reference
only works if both the argument and the lvalue are references to arrays. If an object is passed in, it will be copied and a reference to it will be set in both places. The subsequent reassignment of the lvalue will not have any effect on the original array.
Referencing an existing array and then copying it using clone can work around this. The example below shows how this can be done, but you could also use a factory or some other means to create a new array that has references to all of the existing elements (by calling get_object_vars() on each).
<?php
function getNewList(TestBase $ref) : array
{
$newlist = [3 => clone $ref, 5 => clone $ref];
$newlist[3]->SetTest(3);
$newlist[5]->SetTest(5);
return $newlist;
}
//$ref = clone $listOfTest[4];
$ref = &$listOfTest[4];
var_dump($ref); // TestBase Object (1) (2) { ["m_test"]=> int(5) }
//$newList = getNewList($ref);
foreach ($listOfTest as &$test)
{
echo "before: ".$test->GetTest().'<br>'; // 2, 4, 6 in output here!
var_dump($test); // TestBase Object (1) (2) { ["m_test"]=> int(4) }
echo "after: ".$test->GetTest().'<br>'; // 2, 4, 6 in output here!
var_dump($test); // TestBase Object (1) (2) { ["m_test"]=> int(4) }
}
?>

PHP - adding an object with its properties to array

I have a problem with modifying an array.
foreach ($page->getResults() as $lineItem) {
print_r($lineItem->getTargeting()->getGeoTargeting()->getExcludedLocations());
}
This code gives a result:
Array
(
[0] => Google\AdsApi\Dfp\v201611\Location Object
(
[id:protected] => 2250
[type:protected] => COUNTRY
[canonicalParentId:protected] =>
[displayName:protected] => France
)
)
I'm trying to add another, [1] , same type of object to this array.
I made a class to create and add an object:
class Location{
public function createProperty($propertyName, $propertyValue){
$this->{$propertyName} = $propertyValue;
}
}
$location = new Location();
$location->createProperty('id', '2792');
$location->createProperty('type', 'COUNTRY');
$location->createProperty('canonicalParentId', '');
$location->createProperty('displayName', 'Turkey');
array_push($lineItem->getTargeting()->getGeoTargeting()->getExcludedLocations(), $location);
Then, if I pass this into print_r() function
print_r($lineItem->getTargeting()->getGeoTargeting()->getExcludedLocations());
It shows the same result.
In the end, I need to send this updated whole $lineItem to this function
$lineItems = $lineItemService->updateLineItems(array($lineItem));
But seems like before sending I can't properly add an object to the array.
Thanks in advance.
PHP returns arrays as a value instead of as a reference. This means you must set the modified value back somehow.
Looking at the library apparently in question, there seems to be setExcludedLocations method for that purpose.
So your code should be something like:
$geo_targeting = $lineItem->getTargeting()->getGeoTargeting();
$excluded_locations = $geo_targeting->getExcludedLocations();
array_push($excluded_locations, $location);
$geo_targeting->setExcludedLocations($excluded_locations);

PHP RecursiveIteratorIterator overwrites array keys

Here is the function I wrote to flatten the multidimensional PHP array:
function flattenArray(array $array) {
if (! is_array($array)) {
throw new Exception ("Please specify an array.");
}
$resultArray = [];
$arrayObject = new RecursiveArrayIterator($array);
foreach(new RecursiveIteratorIterator($arrayObject) as $key => $value) {
$resultArray[$key] = $value;
}
return $resultArray;
}
And using it:
$arr = [
["sitepoint", "phpmaster"],
["buildmobile", "rubysource"],
["designfestival", "cloudspring"],
"not an array"
];
print_r(flattenArray($arr));
Result:
Array
(
[0] => designfestival
[1] => cloudspring
[3] => not an array
)
However, I was expecting:
0: sitepoint
1: phpmaster
2: buildmobile
3: rubysource
4: designfestival
5: cloudspring
6: not an array
But it is re-generating indexes as in:
0: sitepoint
1: phpmaster
0: buildmobile
1: rubysource
0: designfestival
1: cloudspring
3: not an array
So how do I modify function to get all elements of the array not just three:
Array
(
[0] => designfestival
[1] => cloudspring
[3] => not an array
)
Thanks for the help
if (!is_array($array)) is superfluous, since you have the array type hint in the function signature and PHP will enforce that.
You are overwriting the keys. Those elements all have the same keys in their respective subarray. Since it's not an associative array, you don't need to preserve the keys. Instead of
$resultArray[$key] = $value;
just do
$resultArray[] = $value;
I too hit this limitation with RecursiveIteratorIterator.
At first I had been using this concise, one-line array flattener wherever needed:
$outputs = iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator([$inputs])), FALSE);
similar to your longer function above.
All was great: I was able to "normalize" my data structure into a 1D array, no matter if the incoming $inputs parameter came into my Symfony2 Controller as a single String/float value, 1D or 2+D multidimensional array. (I was writing a callback from AJAX that is to respond with JSON-formatted tables for an interactive Highcharts.com chart to be able to render, in my financial app.)
However, it refused to draw because in the final step, each data cell was in the form
0 => float 100.662
even though I had taken care that my $inputs creature only contained cells in the form:
'2002-04-30' => float 100.662
So basically the above array-flattening line had killed the keys (DateStamp).
Fed up with studying RecursiveIteratorIterator, I just broke down and came up with my own array_flatten that preserves keys, if any:
static public function array_flatten($inObj)
{
$outObj = []; $inObj=[$inObj];
array_walk_recursive($inObj, function ($incell, $inkey) use (&$outObj)
{
$outObj[$inkey] = $incell;
} );
return $outObj;
}
Note that you are responsible for ensuring that the keys in $inObj are globally unique (and either string or int type), otherwise, I don't know how my function behaves. Probably overwrites the value using the same key name?

How to create array of objects in php

I'm attempting to create an array of objects in php and was curious how I would go about that. Any help would be great, thanks!
Here is the class that will be contained in the array
<?php
class hoteldetails {
private $hotelinfo;
private $price;
public function sethotelinfo($hotelinfo){
$this->hotelinfo=$hotelinfo;
}
public function setprice($price){
$this->price=$price;
}
public function gethotelinfo(){
return $hotelinfo;
}
public function getprice(){
return $price;
}
}
And here is what I am attempting to do-
<?PHP
include 'file.php';
$hotelsdetail=array();
$hotelsdetail[0]=new hoteldetails();
$hotelsdetail[0].sethotelinfo($rs);
$hotelsdetail[0].setprice('150');
?>
The class attempting to create the array doesn't compile but is just a best guess as to how I can do this. Thanks again
What you should probably do is:
$hotelsDetail = array();
$details = new HotelDetails();
$details->setHotelInfo($rs);
$details->setPrice('150');
// assign it to the array here; you don't need the [0] index then
$hotelsDetail[] = $details;
In your specific case, the issue is that you should use ->, not .. The period isn't used in PHP to access attributes or methods of a class:
$hotelsdetail[0] = new hoteldetails();
$hotelsdetail[0]->sethotelinfo($rs);
$hotelsdetail[0]->setprice('150');
Note that I capitalized the class, object, and function names properly. Writing everything in lowercase is not considered good style.
As a side note, why is your price a string? It should be a number, really, if you ever want to do proper calculations with it.
You should append to your array, not assign to index zero.
$hotelsdetail = array();
$hotelsdetail[] = new hoteldetails();
This will append the object to the end of the array.
$hotelsdetail = array();
$hotelsdetail[] = new hoteldetails();
$hotelsdetail[] = new hoteldetails();
$hotelsdetail[] = new hoteldetails();
This would create an array with three objects, appending each one successively.
Additionally, to correctly access an objects properties, you should use the -> operator.
$hotelsdetail[0]->sethotelinfo($rs);
$hotelsdetail[0]->setprice('150');
You can get the array of object by encoding it into json and decoding it with $assoc flag as FALSE in json_decode() function.
See the following example:
$attachment_ids = array();
$attachment_ids[0]['attach_id'] = 'test';
$attachment_ids[1]['attach_id'] = 'test1';
$attachment_ids[2]['attach_id'] = 'test2';
$attachment_ids = json_encode($attachment_ids);
$attachment_ids = json_decode($attachment_ids, FALSE);
print_r($attachment_ids);
It would render an array of objects.
output:
Array
(
[0] => stdClass Object
(
[attach_id] => test
)
[1] => stdClass Object
(
[attach_id] => test1
)
[2] => stdClass Object
(
[attach_id] => test2
)
)

Categories