I have following question:
I am programming a SOAP Server application with PHP. But I have to do that in two different ways, one which is for external usage (for all people) and one that is just for an import. And the import application has just a little bit more possibilities, but else it is the same.
In C I would write something like this (using the preprocessor):
#ifdef INTERNAL
int funktion( int ok, double asdfg, const char *aaa){
#else
int funktion( int ok, double asdfg){
#endif
return 0;
}
I know the function defined()
in PHP, but it does not really do what I want to do (I think).
But is there something simolar?
Of course I could write two different applications, but it would be very great if there was something like this ...
Thank you for help!
EDIT: I know that normally it is possible to write conditional functions like
if(CST){
function asdf($asdf){
}
}
else{
function asdf(){}
}
but I need it in a Class and there it does not work ...
Kind regards!
In PHP there are no such pre-processing constructs as PHP is not compiled. But in PHP classes can be defined conditionally. So you could do this in two steps:
define the class with the full option (3rd argument), but define those sensitive members as protected
instead of public
extend the class conditionally, providing access to the protected
members via a new name, and with the appropriate signature. Other public
members do not have to be mentioned explicitly as they are inherited as usual
Here is an example:
define('INTERNAL', false);
// Define complete class, but with members set to protected
// when they have constraints depending on INT/EXT access
class _myClass {
protected function _funktion ($ok, $str, $id = -1) {
echo "arguments: $ok,$str,$id";
}
public function otherFunc() {
echo "other func";
}
}
// Define myClass conditionally
if (INTERNAL) {
class myClass extends _myClass{
// give public access to protected inherited method
public function funktion ($ok, $str, $id) {
$this->_funktion ($ok, $str, $id);
}
}
} else {
class myClass extends _myClass{
// give public access to protected inherited method, but only
// with 2 parameters
function funktion ($ok, $str) {
$this->_funktion ($ok, $str);
}
}
}
$obj = new myClass();
// if signature has 2 arguments, third is ignored
$obj->funktion(1, 'test', 3);
// other methods are availble
$obj->otherFunc();