2
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;
}
6
  • Can you please show your whole class? Commented Apr 5, 2012 at 20:14
  • stackoverflow.com/questions/1985182/… Commented Apr 5, 2012 at 20:17
  • @Michael..thanks....I think that is better than different names. Commented Apr 5, 2012 at 20:19
  • add default value to your parameter like public function makeTweet( $DatabaseObject, $TextObject, $MessageObject = NULL), this way your last parameter will be optional Commented Apr 5, 2012 at 20:20
  • This is very similar to overloading....works fine in my case. Commented Apr 5, 2012 at 20:22

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

O.K that is the best way...just set the default to null.
@GuyMontag It's PHP we're talking about. Either way is fine with it :)
This would work in most cases but very complex functions would be better off using my examples.
1

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

-1 You can mirror the functionality of an overloaded function with a little know how. See my answer.
Gee thanks, it's not like I had already commented on it.. merely with a little ps. for anyone who read it. And my answer was to the question 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..
You are welcome. Your link is the sixth result in a Google Search. At least you sourced your incorrect answer.
PHP doesn't do function overloading. There are ways to work around that, but a work-around isn't overloading. This answer may not be perfect, but it is technically correct.
-1

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

I upvoted you but this is not the best way in my case. I simply added = NULL and got the equivalent of an overloaded function by adding only an if statement. Much simpler to use NULL for my case. Probably case dependent as this is obviously what func_get_args is used for.
Unless you forget to add null.
This only allows half a work around, what if you want to have the actual function differ? Other than using loops there is no way to handle the extra-parameters anyways..
@RyanS Either of my examples would need some deciphering of the variables inside the function if you wanted to do something different but it would indeed allow you to do different things. I know the OP is doing this when he checks if a variable is null or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.