-1

i want to store function in array

send the array to another page

then execute it

i already read Can you store a function in a PHP array but still don't know what to do

here's what i try

control.php (it start here)

<?php

    function getFirstFunction(){    echo "first function executed"; }

    $data = array();
    $data[0] = getFirstFunction();
    $data[1] = function(){  echo "second function executed";   };
    $data[2] = function(){  require_once "additional.php";    };
    $data[3] = "my string";


    header('Location: view.php?data='.$data);

?>

additional.php

<?php   echo "additional included"   ?>

view.php

<?php
    if( isset($_GET['data']) ){

        foreach( $_GET['data'] as $temp ){
            if( is_callable($temp) ){
                $temp;

            }else{
                "its not a function";
            }
        }
    }
?>

my error =

Warning: Invalid argument supplied for foreach() in D:\Workspace\Web\latihanns\php\get\view.php on line 4

EDIT thanks for notify that this code its dangerous.i'm not use this code in real live. i just try to learn store function in array then call it. then i just curious how if i call it on another page. i just simply curious... i make my code look clear and simple here because i afraid if i wrote complicated code, no one will be here or my post will closed as too localized...

Community
  • 1
  • 1
ilike
  • 99
  • 1
  • 11
  • 2
    `var_dump($_GET['data']);` PS: do you realize that even if it was possible - anyone could pass **anything** they want to invoke on your server. – zerkms Nov 24 '13 at 04:01
  • 2
    You can't just put a raw Array in a URL like that. – maček Nov 24 '13 at 04:02
  • 2
    `$data[0] = getFirstFunction();` This stores the return value of `getFirstFunction()`, not the code, in the data array. – Hanky Panky Nov 24 '13 at 04:05
  • Why can those functions not simply be defined on that second page? – Hanky Panky Nov 24 '13 at 04:06
  • @zerkms when i try var_dump($_GET['data']) it returns string(5) "Array"(i don't know what can i do with this). yes i understand the risk(thanks for notify). i just try to learn store function in array then send it to another page. – ilike Nov 24 '13 at 04:07
  • @ilike: explain the original task. What you want to do at this moment doesn't make any sense. – zerkms Nov 24 '13 at 04:11
  • @zerkms it just for learning purpose since javascript can handle this, i just want to know can php do this kind too.i not use this code in real live – ilike Nov 24 '13 at 04:42
  • @Hanky웃Panky it just for learning.can i do this? – ilike Nov 24 '13 at 04:50
  • @ilike: javascript *cannot* handle this. With JS you cannot serialize an anonymous function with context (see a closure) and transfer it somewhere – zerkms Nov 24 '13 at 04:52
  • @zerkms i talk about json. by the way, i know about php oop. maybe later i will filter the array function in url based on the existing function in another class.but for now i need to know how to call function in array in another page as my question say – ilike Nov 24 '13 at 05:01
  • 1
    @ilike: `json` is not a javascript specific format. Which doesn't support functions serialization as well. You probably confused it with javascript objects. "as my question say" --- I asked *twice*, now will repeat for the last 3rd time: what is the original issue you're trying to solve with such a weird solution? – zerkms Nov 24 '13 at 05:04
  • @zerkms i don't know the issue yet. i just try to learn. later maybe i will relate it with another filtering. and if it dangerous of course i will not use my code. but i need to know this to determine my next step – ilike Nov 24 '13 at 05:17
  • 1
    @ilike: you want to learn something that makes no sense and no one would ever use or what? 1. you can save a reference to an anonymous function in an array 2. you cannot serialize anonymous functions. Is there anything else that's not clear for you? – zerkms Nov 24 '13 at 05:18
  • @zerkms ok. i give up. thanks for notify – ilike Nov 24 '13 at 05:25

1 Answers1

1

If you want to pass anything than string into URL, only option is convert it to string form which is reversible to original types. PHP offers function called serialize() which converts anything to string. After that, you can call unserialize() to convert string back to original data. So you have to change one line in control.php to this:

header('Location: view.php?data='.serialize($data));

In file view.php you have to change one line to this:

foreach( unserialize($_GET['data']) as $temp ){

But you have to fix more things than this. If you have callable variable, you can't invoke function with $variable, but with $variable(). It is good to mention, that in PHP does not matter if you have real function (anonymous function, Closure etc.) in variable, or if variable is simple string with name of exists function.

However you have even another bug in control.php. Code $data[0] = getFirstFunction(); will not pass function getFirstFunction an make it callable, it just calls the function and put its return value to variable. You can define getFirstFunction as anonymouse function like function in $data[1] or just pass it as string like $data[0] = 'getFirstFunction' which will work.

At the end - as anyone mentioned here - IT IS VERY DANGEROUS ans you shouldn't use this on public server.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
micropro.cz
  • 598
  • 4
  • 17
  • So, do you think an anonymous function will be serialized or what? – zerkms Nov 24 '13 at 04:12
  • I just realized, I don't know if anonymouse function (Closure) can be (un)serialized (most probably not). You have to try it. But it will work for sure if you will have these functions defined in view.php and you will pass their names as string into that array. – micropro.cz Nov 24 '13 at 04:14
  • So, what is the point of your whole answer then? PS: I'm not an op – zerkms Nov 24 '13 at 04:15
  • Whole question is kind of pointless, because it is really bad practice to send something via url and execute then. So I used this question to its only usable purpose - to learn something on ridiculous example. I at least informs him about usage of serializable - which was part of that question - how to pass array or other types to URL. – micropro.cz Nov 24 '13 at 04:18
  • And to complete that answer, there is one even dumber but possible way. Instead of anonymouse function save function definition (raw code) into string and after that define and run that function via eval() – micropro.cz Nov 24 '13 at 04:21
  • @micropro.cz i change serialize and deserialize on header. i change $temp to $temp(). i change $data[0] = getFirstFunction(); to $data[0] = function(){ getFirstFunction(); }; i got error = Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'Closure' is not allowed' in D:\Workspace\Web\latihanns\php\get\control.php:11 Stack trace: #0 D:\Workspace\Web\latihanns\php\get\control.php(11): serialize(Array) #1 {main} thrown in D:\Workspace\Web\latihanns\php\get\control.php on line 11 – ilike Nov 24 '13 at 04:22
  • @ilike: what is the original task? The current question doesn't make sense – zerkms Nov 24 '13 at 04:26
  • Yes, as I told, you can't serialize Closure (`$var = function(...)`). You have to define function in view.php and pass its name as string to variable. – micropro.cz Nov 24 '13 at 04:26
  • @micropro.cz can u show me how to? can we skip about serialize and deserialize just to make it clear and simple – ilike Nov 24 '13 at 04:46
  • @zerkms it was the original task. store function in array then call it in another page – ilike Nov 24 '13 at 04:49
  • @ilike: it makes no sense. It's not a task, it's a (terrible) solution for some task – zerkms Nov 24 '13 at 04:52
  • As I mentioned, the only way how to save function into array (or variable) is save directly it's body (code) into variable as string and then call it using `eval()`. – micropro.cz Nov 25 '13 at 19:15