0

I am attempting to call a class function from address bar as follows:

http://localhost:82/spam_fetcher.php?rm=index

My script is:

  class Spam_fetcher{
    public function __construct()
    {
        if (isset($_GET['rm']) && method_exists('Spam_fetcher', $_GET['rm'])) {
            $view = new Spam_fetcher();
            $view->$_GET['rm']();
        }
        else
        {
            echo "No such a function";
        }
    }

    public function index()
    {
        echo 'something';
    }
}

But index function does not execute. What do you guys think I am doing something wrong here?

8
  • 2
    Wrong place: you didn't show your code & where did you call your method Commented Apr 7, 2014 at 12:13
  • What code is executing in your script? Commented Apr 7, 2014 at 12:14
  • 1
    You just define a class, there's nothing actually instantiating or calling it (as far as we can see). Commented Apr 7, 2014 at 12:15
  • I'd like to call index() and echo out 'something' what is your suggestion? Commented Apr 7, 2014 at 12:16
  • $sf = new Spam_fetcher; $sf->index(); Commented Apr 7, 2014 at 12:16

1 Answer 1

2
class Spam_fetcher{
    public function __construct()
    {
        if (isset($_GET['rm']) && method_exists($this, $_GET['rm'])) {
           $this->$_GET['rm']();
        }
        else
        {
            echo "No such a function";
        }
    }

    public function index()
    {
        echo 'something';
    }
}

$view = new Spam_fetcher();
Sign up to request clarification or add additional context in comments.

2 Comments

+1 , You should also explain the changes done on your code like adding $this , created a new instance, removed the new Spam_fetcher();.. etc.
$this->$_GET['rm'](); possible code injection. Never ever do like this without checking user input first or at least escaping it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.