kthxbai2u Posted October 26, 2008 Share Posted October 26, 2008 I was wondering.... I want to build multi-purpose functions or w.e , i was wondering if i can do something like this (sorta like in java or other languages): function foo() { //do something } function foo($a) { //do something } function foo($a, $b) { //do something } $foobar = foo(); $foobar = foo($foobar); $foobar = foo($foobar, foo($foobar)); lol foo [EDIT] Im gonna go try lol.... and put die statements in so i know if its executing the right function [/EDIT] [EDIT]NVM, just found out you cant do that... It basically crashes the script... [/EDIT] Link to comment https://forums.phpfreaks.com/topic/130170-solved-multiple-same-functions-different-argument-sets/ Share on other sites More sharing options...
genericnumber1 Posted October 26, 2008 Share Posted October 26, 2008 No, you can't overload functions in php. You could, on the other hand, just set the default argument values to null, and if it's null you know that parameter wasn't passed.... Of course, you could always use func_get_args() http://us.php.net/manual/en/function.func-get-args.php Link to comment https://forums.phpfreaks.com/topic/130170-solved-multiple-same-functions-different-argument-sets/#findComment-675017 Share on other sites More sharing options...
kthxbai2u Posted October 26, 2008 Author Share Posted October 26, 2008 yeah i just found that this works : function isAdmin($user="") { //Check if $user was set or not if (isset($user) && $user != "") { //user was set, check if $user is an admin } else { //$user was not set, lets see who is currently logged in and check if they are admin. } } Thanks for your help.... I was going to shorten the topic name and add [sOLVED] to it, but I dont think I can edit the original post anymore... Link to comment https://forums.phpfreaks.com/topic/130170-solved-multiple-same-functions-different-argument-sets/#findComment-675021 Share on other sites More sharing options...
genericnumber1 Posted October 26, 2008 Share Posted October 26, 2008 thanks for your help.... What would we call what i was trying to do before? overloaded functions. Link to comment https://forums.phpfreaks.com/topic/130170-solved-multiple-same-functions-different-argument-sets/#findComment-675023 Share on other sites More sharing options...
kthxbai2u Posted October 26, 2008 Author Share Posted October 26, 2008 lol yeah, thanks, i reread your post lol... sorry im flyin lol... To Mods: Might suggest topic name -> [sOLVED] Overloading Functions? I cant seem to change the original post :-/ Link to comment https://forums.phpfreaks.com/topic/130170-solved-multiple-same-functions-different-argument-sets/#findComment-675024 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.