Page 1 of 2

Undeclare a Function

Posted: Sun Jul 01, 2007 9:04 pm
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

Posted: Sun Jul 01, 2007 9:24 pm
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.

Posted: Sun Jul 01, 2007 10:19 pm
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!

Posted: Mon Jul 02, 2007 4:01 am
by volka
php can't do that without the runkit extension.

Posted: Mon Jul 02, 2007 4:36 am
by Oren
We don't need to mention that it's not smart to do so, right?

Posted: Mon Jul 02, 2007 4:42 am
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 ;)

Posted: Mon Jul 02, 2007 5:05 am
by Oren
:?: :? :?:

Posted: Mon Jul 02, 2007 5:15 am
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.

Posted: Mon Jul 02, 2007 5:30 am
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.

Posted: Mon Jul 02, 2007 5:58 am
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.

Posted: Mon Jul 02, 2007 8:23 am
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

Posted: Mon Jul 02, 2007 8:46 am
by volka
I don't think that "different" equals "stupid".

Posted: Mon Jul 02, 2007 9:20 am
by Oren
Depends on the dictionary that you check with :P

Posted: Mon Jul 02, 2007 9:35 am
by Jenk
"Powerful" is what springs to mind, not "Stupid".

Anyway, to avoid yet another Smalltalk vs. Whatever argument... *leaves thread*

Posted: Mon Jul 02, 2007 12:59 pm
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