1

I'm building a front-controller and I need call classes with the params in the $_GET array. So, when I try this in the index.php

<?php
        try{
            if(filter_input_array(INPUT_GET)!==FALSE OR filter_input_array(INPUT_GET)!==FALSE){
                $get = ['module'=>[FILTER_SANITIZE_SPECIAL_CHARS, FILTER_FLAG_STRIP_HIGH],
                        'method'=>[FILTER_SANITIZE_SPECIAL_CHARS, FILTER_FLAG_STRIP_HIGH],
                        'args'=>[FILTER_SANITIZE_SPECIAL_CHARS, FILTER_FLAG_STRIP_HIGH]];
                $args = \filter_input_array(\INPUT_GET, $get);

                //here is the problem syntax
                echo (new \controller\$args['module']())->$args['method']($args['args']);
            }
        } catch (Exception $ex) {
            echo "page not found";
        }
    ?>

The PHP parser give me an error syntax (expected identifier).

So, how can I solve this? Do you know some possible syntax or something different method?

I want just work with OOP. Thank you to everybody (and sorry for my English, my native langague is Spanish).

Thom Wiggers
  • 6,938
  • 1
  • 39
  • 65
beginstack
  • 21
  • 2
  • Try creating a string including the namespace and class name and then doing `$obj = new $clsString()` remember to properly escape the backslashes – Ruan Mendes Feb 21 '15 at 17:13
  • http://stackoverflow.com/help/mcve - Minimal - just follow the steps, and you'll identify the problem. – Karoly Horvath Feb 21 '15 at 17:13

1 Answers1

0

Here's an example where the class name, method name and arguments are dynamic http://goo.gl/Oi6kCU

<?php
namespace foo;
class Cat { 
  function speak($word) {echo "meoow $word\n";}
}
class Dog {
  function speak($word) {echo "woof $word\n";}
}

function callInFoo($cls, $method, $arg) {
    $fullName = "foo\\" . $cls;
    $obj = new  $fullName;
    return $obj->$method($arg);
}

callInFoo("Cat", "speak", "Hello");
callInFoo("Dog", "speak", "World");

Outputs

meoow Hello
woof World

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217