Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP:Array ByRef does not work #480

Open
amitabh-georadius opened this issue May 27, 2021 · 1 comment
Open

PHP:Array ByRef does not work #480

amitabh-georadius opened this issue May 27, 2021 · 1 comment

Comments

@amitabh-georadius
Copy link

I am trying to pass an array ByRef from Php script to a function inside my extension, so that the values of the array changed by the extension function is accessible back in the Php script.

But for some reason, it doesn't seem to be working. I have seen issue #350, so was wondering if this has been fixed, or is there something I am completely missing. As is mentioned in the linked issue, strings, integers etc are working properly when passed ByRef, its only the arrays I am having trouble with

@henrywood
Copy link

henrywood commented Oct 11, 2021

This workaround seems to work with version 2.2.0

Extension:

#include <phpcpp.h>

Php::Value test(Php::Parameters& params) {

	Php::Value theArray = params[0];
	Php::Value theString= params[1];
	
	theArray = theArray.makeReference(); 		// Workaround for this bug
	theArray["THIS IS"] = "YET ANOTHER TEST";
	params[0] = theArray;
	params[1] = "YET AGAIN !";

        std::string dummy = "DUMMY";

	return dummy;
}

extern "C" {
	PHPCPP_EXPORT void *get_module() {

            static Php::Extension extension("test", "1.0");

	    // PHP Signature: function test(array &$input2, string &$input) : string 
            extension.add<test>("test", {	
                    Php::ByRef("input", Php::Type::Array, true),
                    Php::ByRef("input2",Php::Type::String, true)
             });
       }
}

PHP:

$a = ['THIS IS' => 'A TEST', 'I AM' => 'UNCHANGED !'];
$b = "AGAIN";
$dummy = test($a, $b);
var_dump($dummy, $a, $b);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants