php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #44485 array_splice behaves unexpectedly when given an object as the last argument
Submitted: 2008-03-19 20:58 UTC Modified: 2008-03-20 21:19 UTC
From: computerlov at yahoo dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.2.5 OS: windows xp
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
3 + 6 = ?
Subscribe to this entry?

 
 [2008-03-19 20:58 UTC] computerlov at yahoo dot com
Description:
------------
when doing something like this:

$arrOfObjects = array($obj1, obj2....obj10);
$oSomeObject = new CSomeClass();
array_splice($arrOfObjects, 8, 2, $oSomeObject);

instead of getting an array of 9 objects, you get an array of 8 objects and all the data members of CSomeClass in the array.

it's as if we kind of  'var_dumped' the object into the array instead of copying it to the array.

Reproduce code:
---------------
class CSomeClass
{
  private $m_var1;
  private $m_var2;
}

...
$oSomeClass = new CSomeClass();
.. Do Stuff on $oSomeClass ..
$arrObjects = ($obj1, $obj2);
array_splice($arrObjects, 1, 1, $oSomeClass);

Expected result:
----------------
$arrObjects = {$obj1, $obj2, $oSomeClass}

Actual result:
--------------
$arrObjects = {$obj1, $obj2, $m_var1, $m_var2}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-19 21:34 UTC] felipe@php.net
Please, send the complete example. I can't reproduce.

<?php

class CSomeClass {
  private $m_var1;
  private $m_var2;
}

$oSomeClass = new CSomeClass();
$arrObjects = array();

for ($i = 0; $i < 10; $i++) {
	$arrObjects[] = new stdClass;
}

var_dump(array_splice($arrObjects, 8, 2, $oSomeClass));

/* Output:
array(2) {
  [0]=>
  object(stdClass)#10 (0) {
  }
  [1]=>
  object(stdClass)#11 (0) {
  }
}
*/
 [2008-03-19 22:54 UTC] computerlov at yahoo dot com
My complete example is a bit long.

I'm basically creating an array of references to an object like so:

$oEmptyObject = new CObject(NULL);
$nMaxIndex = 64;

for ($nCurIndex = 0; $nCurIndex < $nMaxIndex; ++$nCurIndex)
{
    $this -> m_arrDailyObjects[0][$nCurIndex] = &$oEmptyObject;
}

on a different function i attempted to exchange the first two elements of the given array with one new CObject that actually contained data.
I did it as followed:

// this is an array containing all the objects with no particular order.
$this -> m_arrObjects[count($this -> m_arrObjects)] = new CObject($arrFromDatabseData);

// this is an array that saves the objects in a very specific order.
array_splice($this -> m_arrDailyObjects[0], 0, 2, $this -> m_arrObjects[count($this -> m_arrObjects) - 1]);

this resulted in the unexpected response.

Note: I simplified a my code for a bit by replacing some loops and variables with literal indexes to keep it understandable without having to understand all my loops.

CObject is a class with public and private methods, private data members, and of course a constructor, destructor.
Nothing fancy.

Let me know if you manage to reproduce it.
 [2008-03-19 23:01 UTC] computerlov at yahoo dot com
I just noticed something you did wrong when reproducing the bug:

the error is on the array we splice and not the output.

for example:
$arrInput = {$obj1, $obj2......}

$arrOutput = array_splice($arrInput, .......);

the problem is in $arrInput (which is passed by reference to array_splice) and not $arrOutput.
 [2008-03-20 10:58 UTC] felipe@php.net
> I just noticed something you did wrong when reproducing the bug:
> the error is on the array we splice and not the output.

Oh, ok!

> instead of getting an array of 9 objects, you get 
> an array of 8 objects and all the data members of 
> CSomeClass in the array.

The data of 'replacement' parameter is converted to array, then, the object is converted to an array of their properties. This is expected.


Thanks.
 [2008-03-20 11:02 UTC] felipe@php.net
The documentation describe the fourth parameter as array, but the correct is mixed.

Reclassified as Documentation problem.
 [2008-03-20 13:44 UTC] computerlov at yahoo dot com
so If i do this

$arr = array($oSomeObject);

array_splice(..., 0, 2, $arr);

will it work as I intended?
I'll check that later.
 [2008-03-20 14:02 UTC] felipe@php.net
Yes, exactly.
 [2008-03-20 16:35 UTC] computerlov at yahoo dot com
Just checked it on my machine.
worked exactly as expected.

Instead of passing the $object as a parameter to array_splice, I passed array($object). Then the object itself replaced the others in the array and not its data members.
Thank you.
 [2008-03-20 21:19 UTC] ezyang@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Sep 19 12:01:26 2019 UTC