Mark
Mark

Reputation: 155

function_exists php

I'm trying to write a function that could receive it's second arg a name of a function. In order to validate function's name (that will be used in an eval statement) I use function_exists such as:

if(function_exists($type)){
    $toEval= $type.'(\''.$file.'\');';
  }else{

But if the arg sent is include or require (with 'once' variations) this code fails (function_exists returns false).

Upvotes: 0

Views: 1349

Answers (3)

Álvaro González
Álvaro González

Reputation: 146660

Those are not functions but language constructs:

http://mahmudahsan.wordpress.com/2008/07/02/php-take-advantage-of-language-constructs/

If they're acceptable, you should filter them out manually, e.g. using a switch statement:

switch( strtolower($type) ){
    case 'echo':
    case 'print':
    case 'include':
       ...
       break;
}

Upvotes: 0

Pekka
Pekka

Reputation: 449813

include, require and a number of other calls are language constructs, not functions.

You would have to check for these manually.

See this manual page for a list of them.

Upvotes: 1

Matěj Zábský
Matěj Zábský

Reputation: 17272

I think you should think whether your application is really designed well. This kind of code often suggests there is something wrong with the overall design. You should also be very careful what you pass into such method. You may end up with some very serious security issues.

if($type == 'require' || $type == 'require_once' || .. || function_exists($type)){}

Or better

$constructs = array('include', 'require', ...);
if(in_array($type, $constructs) || function_exists($type)){};

Upvotes: 2

Related Questions