undeclare a function??

A

Anonymous

Guest
if I declare a function can I undeclare it later, so I can declare another function under that name??

It should look a something like this:

Code:
function do_something()
{
    $a = 1;
    return $a;
}

print(do_something());

undeclare('do_something');

function do_something()
{
     $b = 6;
     return $b;
}

print(do_something());
this would produce this:


Greetz Daan
 
Why on earth would you want to do such a thing? There is a way of checking whether a function exists, although it escapes me at this moment. Have you tried simply rewriting the function later in the page?
 
Sorry, but I hope NOT!

What on earth would you want to use that for???

Pim
 
dvdbinternet said:
if I declare a function can I undeclare it later, so I can declare another function under that name??

It should look a something like this:

Code:
function do_something()
{
    $a = 1;
    return $a;
}

print(do_something());

undeclare('do_something');

function do_something()
{
     $b = 6;
     return $b;
}

print(do_something());
this would produce this:


Greetz Daan
Hi You can try this:
create file some like this: functions_add.php and functions_del.php
in both create function with one name functions;
in main script use include_once.

Good Luck!
 
I want to do this to prevent a lot of code wich is almost the same. If I could replace a function (of my own) with another it would keep the number of lines low.
And in this case i'm talking about about 1500 lines of PHP(!) code.

rewriting the function later on the page gives an error message that the function allready exists.
I can't declare the function later, because it is used before that point.

Hi You can try this:
create file some like this: functions_add.php and functions_del.php
in both create function with one name functions;
in main script use include_once.
this does not undeclare a function, it simply declares the function in one of the files. I cannot simply use the other file.....
On top of that, the functions come from the database. (but you couldn't know that)

After reading here that nobody knows how to do that, and after searching in the manual for about an hour and not finding anything. I will have to find some other solution. Wich probably results in complete rewriting the script :(

oh, well, as long as I don't run out of coffee :wink:

Greetz Daan
 
dvdbinternet said:
I want to do this to prevent a lot of code wich is almost the same. If I could replace a function (of my own) with another it would keep the number of lines low.
I'm sure that if it were a case of a lot of the code being identical, you can throw a variable into the function to get it to change it's process slightly!
 
just throw a function to a variable, example if you have
Code:
function AlmostIdentical(){
    echo('Almost');
    echo('Identical');
    echo('1');
}
AlmostIdentical();
undeclare('AlmostIdentical');
function AlmostIdentical(){
    echo('Almost');
    echo('Identical');
    echo('2');
}
AlmostIdentical();

just rewrite it as
Code:
function AlmostIdentical($mode){
    echo('Almost');
    echo('Identical');
    echo($mode);
}
AlmostIdentical(1);
AlmostIdentical(2);

if there was a "undeclare" function to undeclare functions, both of the examples above would print out "12"

BTW, did you notice that undeclaring a funcation and redeclaring it later would mean more lines of code than just having 2 functions?

for example if you have a function that occupies 5 code lines and another that occupies 7 code lines. without the calling code you have a total of 12 lines, now you want an undeclare function to "minimize" the lines of code...

so you declare the first function, 5 lines. undeclare it, add one line. declare another function with the same name as the first (redeclare, if you want) add 7 lines... that would be a total of 13 lines

i think 12 is more compact than 13, don't you agree?
 
Back
Top