public function makeTweet( $DatabaseObject, $TextObject, $MessageObject)
{
if( $DatabaseObject == NULL )
{
$DatabaseObject = new Database();
$TextObject = new Text();
$MessageObject = new Message();
}
$TweetObject = new ControlTweet();
$TweetObject->setObjects($DatabaseObject, $TextObject, $MessageObject);
return $TweetObject;
}
3 Answers
You can add optional parameters in the function declaration like:
public function makeTweet( $DatabaseObject, $TextObject, $MessageObject = null)
Now you can either do:
$obj->makeTweet($db, $text, $messageObj);
or
$obj->makeTweet($db, $text);
This is the closest you can get in PHP.
3 Comments
You cannot overload a function in PHP. See this page for reference: http://www.daniweb.com/web-development/php/threads/19978/overloading-php-functions
4 Comments
What is the correct way to overload functions in PHP so I didnt bother going into too much detail. Instead I provided a link for further reading if the OP was interested. Thanks anyways..PHP functions can be "overloaded". Use func_get_args and set no variables in the function.
You could also submit an associative array as a single variable. Then you can use extract inside the function to make friendly variables.
$vars = array('key1'=>'value1','key2'=>'value3');
function function_name($v){
extract($v);
//do something
}
For the function to behave differently you would need to determine what your variables are. In this way you can mirror the overloading idea.
4 Comments
null.null or not.
public function makeTweet( $DatabaseObject, $TextObject, $MessageObject = NULL), this way your last parameter will be optional