| 181 | |
| 182 | === A remark about overloaded methods === |
| 183 | |
| 184 | Avoid introducing overloaded methods or methods that may have overloaded variants in the future to the public API surface if possible. Using an "options" parameter class pattern can future-proof the need to introduce new overloads down the road as you can just introduce new methods on the "options" class itself and update your implementation to handle the new option class methods accordingly. |
| 185 | |
| 186 | If you have to use overloaded methods, do not introduce an overload to a virtual method whose new signature only exists on the derived class and not its base. This makes such a class difficult to wrap for our PHP language target and requires a language-specific SWIG `%rename` workaround. |
| 187 | |
| 188 | An example of where such a scheme made PHP class generation difficult is in the `GetDistance` method of `MgCoordinateSystemMeasure`. It's parent `MgMeasure` class defines `GetDistance` with this signature: |
| 189 | |
| 190 | {{{ |
| 191 | class MgMeasure |
| 192 | { |
| 193 | public: |
| 194 | ... |
| 195 | virtual double GetDistance(MgCoordinate* coord1, MgCoordinate* coord2) = 0; |
| 196 | }; |
| 197 | }}} |
| 198 | |
| 199 | In `MgCoordinateSystemMeasure`, this `GetDistance` method exists in 2 overloaded forms |
| 200 | |
| 201 | {{{ |
| 202 | public: |
| 203 | ... |
| 204 | virtual double GetDistance(MgCoordinate* coord1, MgCoordinate* coord2) = 0; |
| 205 | virtual double GetDistance(double x1, double y1, double x2, double y2)=0; //New overload only in MgCoordinateSystemMeasure |
| 206 | }}} |
| 207 | |
| 208 | When generated by SWIG as-is, the .net and Java proxy classes are correct, but the PHP proxy class for `MgCoordinateSystemMeasure` will cause the PHP binding to throw a PHP fatal error of the form: |
| 209 | |
| 210 | ``` |
| 211 | PHP Fatal error: Declaration of MgCoordinateSystemMeasure::GetDistance(MgCoordinate|float|null $arg1, MgCoordinate|float|null $arg2, float $arg3, float $arg4): float must be compatible with MgMeasure::GetDistance(?MgCoordinate $arg1, ?MgCoordinate $arg2): float in Unknown on line 0 |
| 212 | ``` |
| 213 | |
| 214 | The remedy was to add a SWIG `%rename` directive to the PHP language binding configuration for this one particular method signature to rename the method to `GetDistanceSimple` avoiding the resulting method signature conflict error above. |
| 215 | |
| 216 | By avoiding introducing overloaded methods and preferring the use of an "options" class instead, we avoid this PHP-specific problem and ensure consistent SWIG wrapper generation across our supported language targets. |