Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Run function from variable PHP

Featured Replies

Ok so the basics of what I have is a function call stored in a variable e.g.

   <?php
     $bar = 'doSomething(4)';
   ?>

 

and the function call stored in the variable I need it to call the function in the same script, so I have been trying:

   <?php
     eval('aClass::'.$bar);

   class aClass {
     function doSomething($foo) {
       echo $foo * $foo;
     }
   }
   ?>

 

The php script doesn't execute because of the eval() function, am I doing something wrong here?

 

Is there another way to go about doing this?

 

Thank-you

Jon

Could it be a lack of end of line terminator (semi-colon) inside the eval? E.g.:

 

$bar = 'doSomething(4);';

Just a thought.

 

Or you could equivalently do:

 

eval("aClass::{$bar};");

 

Also, you might want to declare the class method as "public static function doSomething($foo)" otherwise it's an instance method and can only be invoked on an object, not on the class itself.

Edited by Connetu_C

eval is evil!

 

you dont need to use eval for what your trying todo... and in fact if your only left with eval as the only option theres something wrong with your script!

 

also since your using static calling methods your function needs tobe declared as static... i have not tested it but the below should do somthing

 

  <?php

$bar = 'doSomething(4)';

    aClass::{$bar};

   class aClass {
     function static doSomething($foo) {
       echo $foo * $foo;
     }
   }
   ?>

Edited by SniderDK

  • Author

It was the end line terminator in the eval() method. All works now.

 

Thanks for the reply's!

 

and I agree eval() method is evil, I tried putting the variable in parentheses like you showed but It didn't work. Theres nothing wrong with the script as its as simple as anything at the moment. but thanks for the reply!

well... i do call static methods using safe strings in places in my code... if you actually want to know how todo it without eval i can dig about and find it... i haven't touched that code in a fair few months so its not as fresh in my mind as it once was

  • Author

That would be really useful.

 

See what I'm trying to do is using some Ajax. The js on the front end is utilising Ajax to call PHP functions, so I thought the best way for me to do this is pass the function to be called through the Ajax via get and then the php gets this and calls the function. Hope that made sense.

 

Is this the best way to go about doing it?

 

Thanks again Mate!

<?php

//example post var stuff
$_POST['json'] = '{"class":"aClass","method":"doSomething","params":[9]}';

$ajax = json_decode($_POST['json']);

class aClass {
   function doSomething($foo) {
       echo $foo * $foo;
   }
}

call_user_func_array(array(new $ajax->class, $ajax->method), $ajax->params);

 

That code is not for a production environment! You will really want to limit what functions the user can call!

  • Author

Thanks for the reply,

 

Very interesting! I've never used any JSON before but am going to give it a try. I've had that worry in my head knowing that someone malicious could potentially use their own js to call a function I didn't intend them to call.

 

Would you be able to explain how using your code would be harder to manipulate that just standard js string?

 

Sorry if anything sounds a bit noobish here!

 

Thanks

Well if a class/method/function does not exist then call_user_func_array will bail out with a warning. However if you're just evaluating a string as code, you're leaving your website wide open to abuse.

 

e.g

 

       class aClass {
         public static function doSomething($foo) {
           echo $foo * $foo;
         }
       }

     $bar = 'doSomething(4); $output = shell_exec(\'ls -al\'); echo "<pre>$output</pre>";';
     eval('aClass::'.$bar);

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.