Jump to content

[SOLVED] Multiple (same) functions, different argument sets


kthxbai2u

Recommended Posts

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  ;D

 

[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]

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

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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.