undeclare a function??

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
DoppyNL

Wed Aug 14, 2002 6:33 am

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: Select all


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:
16
Greetz Daan
Jay

Wed Aug 14, 2002 8:13 am

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?
Pim
New php-forum User
New php-forum User
Posts: 18
Joined: Tue Aug 06, 2002 3:51 am
Location: Netherlands

Wed Aug 14, 2002 8:14 am

Sorry, but I hope NOT!

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

Pim
User avatar
WiZARD
Moderator
Moderator
Posts: 1240
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol
Contact:

Wed Aug 14, 2002 10:44 pm

dvdbinternet wrote: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: Select all


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:
16
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!
"Sex,Drugs and Rock&Roll " replaced at "Sucks,Bugs and Plug&Play";
Image
DoppyNL

Wed Aug 14, 2002 11:42 pm

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
Jay

Thu Aug 15, 2002 3:23 am

dvdbinternet wrote: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!
japaalekhin
New php-forum User
New php-forum User
Posts: 9
Joined: Thu Nov 26, 2009 8:07 am
Location: Leyte, Philippines
Contact:

Thu Nov 26, 2009 9:27 am

just throw a function to a variable, example if you have

Code: Select all

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: Select all

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?
Post Reply