473,387 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Dynamically invoke method / function

Markus
6,050 Expert 4TB
Say the name of the function is passed by GET.

How can we invoke the method?

EDIT: call_user_func() does the trick?
Jun 4 '08 #1
10 1343
Atli
5,058 Expert 4TB
call_user_func() should work.

You could always use the eval function.
I would put a huge warning sign on the idea of calling functions from a variable passed by get tho!
Just imagine... page.php?func=unlink("~/*");
Dunno if that exact syntax actually works but something similar probably does.
Jun 5 '08 #2
pbmods
5,821 Expert 4TB
Heya, Markus.

Consider validating your function name.

E.g.:
Expand|Select|Wrap|Line Numbers
  1. switch( $_GET['func'] )
  2. {
  3.     case '':
  4.         $func = 'strip_tags';
  5.     break;
  6.  
  7.     case 'strip_tags':
  8.     case 'trim':
  9.     case 'rot13':
  10.         $func = $_GET['func'];
  11.     break;
  12.  
  13.     default:
  14.         $func = 'strip_tags';
  15.     break;
  16. }
  17.  
  18. echo $func($text);
  19.  
[EDIT: I can't wait for that issetor operator....]
Jun 5 '08 #3
Markus
6,050 Expert 4TB
I'm not stupid guys ;]

Like an MVC, the class is passed through the url and the method follows.
Jun 5 '08 #4
hsriat
1,654 Expert 1GB
Say the name of the function is passed by GET.

How can we invoke the method?
Do you mean this...
Expand|Select|Wrap|Line Numbers
  1. if (function_exists($_GET["function"]))
  2. $_GET["function"]();
You can change it for the function arguments too.
Jun 5 '08 #5
Markus
6,050 Expert 4TB
Do you mean this...
Expand|Select|Wrap|Line Numbers
  1. if (function_exists($_GET["function"]))
  2. $_GET["function"]();
You can change it for the function arguments too.
$_GET['func']();
Looks abit ugly, to me.

:)
Jun 5 '08 #6
hsriat
1,654 Expert 1GB
$_GET['func']();
Looks abit ugly, to me.

:)
LOL!!
Does that count if its working?
Jun 5 '08 #7
Markus
6,050 Expert 4TB
LOL!!
Does that count if its working?
Most certainly does!

I like my code like my women ;]
Jun 5 '08 #8
hsriat
1,654 Expert 1GB
Most certainly does!

I like my code like my women ;]
women? ... interesting...
Jun 5 '08 #9
Markus
6,050 Expert 4TB
women? ... interesting...
Is that an offer?
Jun 5 '08 #10
hsriat
1,654 Expert 1GB
Is that an offer?
no.. its already scarcity here.
Jun 6 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Steven D'Aprano | last post by:
Suppose I create a class with some methods: py> class C: .... def spam(self, x): .... print "spam " * x .... def ham(self, x): .... print "ham * %s" % x .......
4
by: Ahmet | last post by:
Hi all; I have one application in which I read form names from database to be opened. I open form with the code below, and call its show method for form to be shown but before this, I must set...
4
by: Jonathan Roewen | last post by:
Hi I've got loading assemblies dynamically done (wasn't too difficult). Now I want to lookup a static function in the loaded assembly, and if found, return it somehow, and call it from my app. So...
15
by: =?Utf-8?B?VG9tIENvcmNvcmFu?= | last post by:
I've been led to believe by several articles, particularly Eric Gunnerson's C# Calling Code Dynamically, that calling a method dynamically through Reflection was much slower than through a...
4
by: =?Utf-8?B?QWJoaQ==?= | last post by:
I am using Reflection to invoke methods dynamically. I have got a special requirement where I need to pass a value to method by setting the custom method attribute. As I cannot change the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.