Undeclare a Function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Macai
Forum Newbie
Posts: 2
Joined: Sun Jul 01, 2007 8:55 pm

Undeclare a Function

Post by Macai »

This may sound quite weird, but I'm curious if it is possible to "undeclare" a function. What I mean by this is, take a user function which has already been declared, and then remove it from existence so that it might be redeclared as something else. (If you didn't notice yet, I am using the eval() function)

Here's an example what I want to do:

Code: Select all

function SomeFunc() {
	//code here
}

undeclare('SomeFunc'); // this is a hypothetical function
SomeFunc(); //try to call a function that no longer exists
The desired result would be:

Code: Select all

Fatal error: Call to undefined function SomeFunc() in ... on line ...
Is there any way this can be accomplished?

I checked the documentation's section on Function Handlin Functions, but alas, there was nothing there. Is there perhaps some sort of workaround or the such that one of you guys can think of?

Thanks
--Macai
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

http://us3.php.net/runkit

Requires a PECL package installation, but there is the runkit_function_remove() in there.

Though in honesty, I'd revaluate your design if you are that heavily reliant on eval() and appear to be allowing user input as a parameter.
Macai
Forum Newbie
Posts: 2
Joined: Sun Jul 01, 2007 8:55 pm

Post by Macai »

Actually, it's not quite what I'm doing.

The design is actually an application that runs entirely in PHP. I wanted the files I write to update automatically in the mid-runtime script. Thanks, though!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

php can't do that without the runkit extension.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

We don't need to mention that it's not smart to do so, right?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Oren wrote:We don't need to mention that it's not smart to do so, right?
Tell that to a Smalltalk coder ;)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

:?: :? :?:
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Surely this is possible without the runkit stuff...

Code: Select all

$addition = create_function('$a,$b','return ($a+$b);');
$sum = $addition(5,3);
unset($addition);

echo $sum; // 8
Ok, it's not technically unsetting a function, just unsetting a handle to an anonymous function, but the effect would be the same.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Oren wrote::?: :? :?:
It's common practice to dynamically create and destroy objects. There is also no bounds to what objects you may destroy at runtime; even the super-objects Object or ProtoObject (the parent of Object)

However, as you can imagine, it's not intelligent to do so for 99% of the time.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Even classes are objects that can be created, modified and deleted at runtime in Smalltalk. And it's not considered bad practice in general.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Oh... I see... Smalltalk is a stupid programming language right? Yeah then tell that to Scheme too. The 2 can run a stupidity contest...
With Scheme, you can overwrite stuff like primitive procedures [e.g overwrite + so (+ 1 2) will return -1 instead of 3 for example... and yes, that's how you'd add 1 to 2 in Scheme: (+ 1 2)]. In scheme you can even overwrite the boolean values... So you can swap TRUE with FALSE for example :oops:

Edit: Oh... Wikipedia on Smalltalk said: "Influenced by: Lisp, Logo; Sketchpad, Simula". Scheme was influenced by Lisp too, now that explains everything :P
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I don't think that "different" equals "stupid".
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Depends on the dictionary that you check with :P
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

"Powerful" is what springs to mind, not "Stupid".

Anyway, to avoid yet another Smalltalk vs. Whatever argument... *leaves thread*
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

"Powerful"? :lol: I want to see how you code a simple counter in Scheme - trust me, it's too ugly than it should be... and I haven't said a word about readability... ever seen a piece of code in Scheme? :P
Post Reply