-1

I just recently discovered variable variables in PHP, needless to say, it´s usefulness is immense:

$name = "ABC";
$$name = "DEF";
echo ${"ABC"}; //Gives: DEF

That got me thinking, which brings us to my question:

Since we can have names that´s variable, can´t we also have functions that´s variable? Not 'functions' as in the names of functions, but (more or less) as in:

$func = 'function test() { echo "Success!"; }';
$func(); //If this would work, it would give: Success!

Or, even better, variables classes:

$class = 'class { function test() { echo "Success!"; } }';
$instance = new $class;
$instance->test(); //In a (not-so) perfect world this would give: Success!

Any of this possible?

Marcus Hansson
  • 816
  • 2
  • 8
  • 17
  • Yes, you are so true (Or, well, the variable _variables_ at least lets you avoid the 324256 lines long declaration of _GET, _POST and what-not variables, and instead simply access them with ${"something"}, or $$something). But, bad feature or not, it´s fun as heck to play around with :D – Marcus Hansson Sep 08 '11 at 07:52
  • what POST variable you going to access using $$? It seems you have missed something. – Your Common Sense Sep 08 '11 at 08:00
  • You could make a function which simply sets a variable _variable_ for each string in an array you feed the function. Like so (next comment): – Marcus Hansson Sep 08 '11 at 08:03
  • function set-get($array) { foreach ($array as $part) { global $$part = $_GET[$part]; //with security and what-not added } } – Marcus Hansson Sep 08 '11 at 08:07
  • what security you're talking about? – Your Common Sense Sep 08 '11 at 08:20
  • as for the *long declarations* I am just wondering, why are you so eager to write *long lists* of separate variables instead of looping them over array. – Your Common Sense Sep 08 '11 at 08:22
  • The security I mean is the usual escaping of $_GET-variables, or whatever you want to use to secure against bad input. And well, can't say I'm _eager_ to do it, but it has its uses. – Marcus Hansson Sep 08 '11 at 08:41
  • what is "usual escaping of $_GET-variables"? There is no such thing. there is no "bad inputs" as well. – Your Common Sense Sep 08 '11 at 08:50
  • `it has its uses` - no, it doesn't. If you have as many variables as it require some automatic processing - you don't need them as separate variables. just keep them as array members and use in a loop. That's the point. – Your Common Sense Sep 08 '11 at 08:54
  • Well, it's no point arguing about it. If you don't like it, don't use it. It's exactly as simple as that. – Marcus Hansson Sep 08 '11 at 12:21

3 Answers3

2

needless to say, it´s usefulness is immense

You cannot be more wrong.
There is absolutely nothing great about variable variables. You'd better discovered arrays

PHP has variable function names as well and it's a bit more usable but still it makes reading your code a torture. So, better to avoid them as well.

Remember the fate of most-known write-only language. PHP ate it in one gulp, only because of PHP's readability. Do not try to make Perl out of PHP. Writing code is leisure but hunting down errors in it - is a REAL job. Don't make your job harder.

Do not write obscure and puzzled code.
Write straight and clean code.
You'd make a huge favor to yourself and other people who happen to work with it.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • This was one quite useless answer. Just as asking for advice on SO would many times be considered off-topic, I would consider this answer quite off-topic. Sorry, but I actually didn´t ask for a 'best practice'-sort of answer. – Marcus Hansson Sep 08 '11 at 07:42
  • @Marcus unfortunately, SO **is not** your personal answering service. And every answer is intended to be used by many people, not only you. – Your Common Sense Sep 08 '11 at 08:05
  • Why? Two simple reasons to why it should get your down-vote: 1. It doesnt answer the original question and 2. It simply says "Do not do this" without providing any other information (such as alternate ways to achieve what I wanted (Such as @oezi answer, who _actually tried to be useful_)). – Marcus Hansson Sep 08 '11 at 08:11
  • But what if someone, like me, wanted to know this _just to get to know the language a little better_? Then this answer would still be utterly useless. You, or anyone else here for that matter, is _not the all-knowing PHP/[Insert language]-god_. You might be experts, or even the creators themselves, but I promise you, there **is** someone who can implement something useful and perfectly readable, maintanable and debuggable with _a correct answer to the original question_. – Marcus Hansson Sep 08 '11 at 08:14
  • @Marcus well they would use other answers. I still can't get what's wrong with mine. – Your Common Sense Sep 08 '11 at 08:20
  • Well, as I said, it doesn't provide any answer to the question. Look at @gilden's answer for example, he basically says: "Here's the method you're asking for, but be aware of how dangerous it is". – Marcus Hansson Sep 08 '11 at 08:33
1

don't know about your third example, but your second one should almost work (assuming you're using php 5.3 or higher ;) ). just leave yout the quotes and the functions name:

$func = function() { echo "Success!"; };
$func(); //should give: Success!

search for "anonymous function" or, to read more about this, just take a look at the documentation.

oezi
  • 51,017
  • 10
  • 98
  • 115
  • Although this would be useful to a beginner, I do know how to use it and what an anonymous function is. Unfortunately, this was not what I was asking for :) – Marcus Hansson Sep 08 '11 at 07:38
  • so if you really want to convert a string containing code (a function, a class or whatever) to actual code, you'd have to stick to `eval` wich you should use only very carefully. for more information, take a look at this: http://stackoverflow.com/q/951373/288773 – oezi Sep 08 '11 at 07:50
  • Well, I probably wouldn't want it in production-code. But, as said before, every little knowledge about the language is useful. You never know, it might come in handy sometime. Oh, and that post was some interesting read! – Marcus Hansson Sep 08 '11 at 08:24
-1

The only way this would work is using eval.

$func = "echo 'Success!';";
eval($func);

You can use variables to call functions as well.

function foo()
{
    echo 'Success';
}

$foo = 'foo';
$foo();

Keep in mind - you're entering a dangerous area of PHP as this usually only obfuscates your code for any future developers having to maintain your code (including yourself in a few months).

kgilden
  • 10,336
  • 3
  • 50
  • 48
  • Thank you, this is most useful (Or, well, I don´t think variable _functions_ or variable _classes_ would be as useful as variable _variables_. But hey, it's always fun to learn new stuff, regardless of whether you're going to use it or not!). – Marcus Hansson Sep 08 '11 at 07:45
  • My thought exactly. It doesn't hurt to know language-specific features - might come in handy (e.g debugging others' code). Using these features yourself is in my opinion absolute madness! – kgilden Sep 08 '11 at 07:56
  • Mostly, yes. But there a few cases where it's actually useful, or convenient at least (See my comment on the question, with thorough documentation it's quite nice to have actually). – Marcus Hansson Sep 08 '11 at 08:20
  • Well, I cannot agree with you. It should never be used other than experimenting. The convenience is just not worth the mess it creates. – kgilden Sep 08 '11 at 08:22