Just like most other languages, you can pass arguments to your extension functions either by value or by reference. By default, all arguments are assumed to be passed by value. We introduce two more PHP-CPP classes now:
Php::ByVal and Php::ByRef
A ByVal argument passed to a function only has local scope inside the function. Whatever manipulation is done to the data within the function does not affect its value outside the function from where it was called because the compiler makes a copy of the data and sends it to the function rather than send the actual data.
A ByRef argument has scope outside the function even after the function has finished executing. That means if the argument data is changed within the function, when the control returns to the calling code, the data will contain the changed value instead of the value which was passed into the function.
The compiler sends the original data into the function for a ByRef argument so its new value is available after the function has finished executing.
Function arguments can be specified in the extension.add() method as a third argument
The first argument is the name of the argument, the second defines the type and the third argument defines whether the argument is required or is optional. By default , all arguments are required.
Lets add two functions to our helloworld extension. fixedArguments() will take in two numbers and return their sum. fixedArgumentsByRef() will do the same thing but return the sum back in the first argument.
Both the functions assume that the arguments passed are of the type int. What happens if you pass non-integers as arguments? Eg. fixedArguments(“xxx”, “yyy”). No error or exception will be thrown. Since PHP-CPP will not be able to take the values passed as integers it will use the default values of int for the parameters so it will become 0+0 = 0.
How i can pass an associative array like an object to the function from php file to the extension? then how i will receive it in extension. am doing this:
@mubin. That is correct. You can pass a PHP array using Php::Type::Array and in the C++ code where you implement the function, you handle param[0] as a std:vector. This way you can iterate through the elements. You have to put in type checking , otherwise blindly assuming values to be present will crash the code.
Can you explain the difference between pass by value and pass by reference a little bit more? I have few confusions there.What is the motive of this line in pass by reference function:
params[0] = arg1;
@mubin. When you pass arguments by reference, their values can be changed from inside the called function. When you pass by value, the original values of the arguments cannot be changed from within the function.
How i can pass an associative array like an object to the function from php file to the extension? then how i will receive it in extension. am doing this:
myExtension.add(“binary”, binary, {
Php::ByRef(“number”, Php::Type::Array)
});
IS that right?
@mubin. That is correct. You can pass a PHP array using Php::Type::Array and in the C++ code where you implement the function, you handle param[0] as a std:vector. This way you can iterate through the elements. You have to put in type checking , otherwise blindly assuming values to be present will crash the code.
Can you explain the difference between pass by value and pass by reference a little bit more? I have few confusions there.What is the motive of this line in pass by reference function:
params[0] = arg1;
@mubin. When you pass arguments by reference, their values can be changed from inside the called function. When you pass by value, the original values of the arguments cannot be changed from within the function.