AMFPHP issue referencing ByteArray

Recently I made some test in AMFPHP sending an retrieving ByteArray to be stored in a mysql blob field.

But suddenly I encounter a problem when I was trying to return an array of ByteArrays and I could not find out the reason flash continued to show me this error:

The php file which was generating this error was:

class Service
{
public function getArray( )
{
$GLOBALS['amfphp']['encoding'] = 'amf3';
$a = new ByteArray("\x00\x10this is a string");
$b = new ByteArray("\x00\x16this is another string");
$c = $a;
return array( $a, $b, $c );
}
}

While the Actionscript test class was:

package
{
import flash.display.Sprite;
import flash.net.NetConnection;
import flash.net.ObjectEncoding;
import flash.net.Responder;
import flash.utils.ByteArray;

public class test extends Sprite
{
private var nc: NetConnection;
private var rsp: Responder;

public function test()
{
rsp = new Responder( onResult, null );
nc = new NetConnection();
nc.objectEncoding = ObjectEncoding.AMF3;
nc.connect( "http://localhost/amfphp/trunk/gateway.php" );
nc.call( "test.bytearray.Service.getArray", rsp );
}

private function onResult( e: * ): void
{
if ( e is Array )
{
for each ( var b: ByteArray in( e as Array ) )
{
trace( 'reading bytearray...' );
b.position = 0;
trace( b.readUTF() );
}
}
}
}
}


After some investigation I found that the error was into the AMFSerializer.php file in the writeAmf3ByteArray method which doesn’t reference correctly the bytearray objects.

Thus I made a little modification and after that I succeeded to retrieve correctly the bytearray list ( with the 3rd element as reference of the first one ).

Here you can download the patch file, if you want to test the modification ( at your own risk 🙂 ). The patch has been created using the last svn version of amfphp and you can apply the patch with the following command:
patch -p0 -i bytearray.patch