-1

I don't have alot of experience with OOP programming in PHP, and my search has given no result but solutions to direct methods. What I need is this:

// URL Decides which controller method to load
$page = $_GET['page'];

// I want to load the correct controller method here
$this->$page();

// A method
public function home(){}

// Another method
public function about(){}

// e.g. ?page=home would call the home() method

EDIT: I've tried several of the suggestions, but what I get is a memory overload error message. Here is my full code:

<?php

class Controller {

    // Defines variables
    public $load;
    public $model;

    public function __construct() {

        // Instantiates necessary classes
        $this->load     = new Load();
        $this->model    = new Model();

        if (isset($_GET['page'])) {

            $page = $_GET['page'];

            $fc = new FrontController; // This is what crashes apparently, tried with and without ();

        }

    }

}

4 Answers4

3

If I understand your question correctly, you'd probably want something more like this:

class FrontController {
    public function home(){ /* ... */ }
    public function about(){ /* ... */ }
}

$page = $_GET['page'];
$fc = new FrontController;
if( method_exists( $fc, $page ) ) {
    $fc->$page();
} else {
    /* method doesn't exist, handle your error */
}

Is this what you're looking for? The page will look at the incoming $_GET['page'] variable, and check to see whether your FrontController class has a method named $_GET['page']. If so, it will be called; otherwise, you'll need to do something else about the error.

Ryan
  • 925
  • 2
  • 6
  • 25
0

You can call dynamic properties and methods using something like this:

 $this->{$page}();
datasage
  • 19,153
  • 2
  • 48
  • 54
0

Use a class.

Class URLMethods {
  public function home(){ ... }
  public function about(){ ... }
}

$requestedPage = $_GET['page'];

$foo = new URLMethods();
$foo->$requestedPage();
Mr Griever
  • 4,014
  • 3
  • 23
  • 41
  • but allowing url variables to control flow is a terrible idea due to security, etc. If you intend to go through with this, make sure you sanitize (explicitly check for allowed values) the GET variable. – Mr Griever Jul 19 '11 at 17:13
-1

You can achieve this by using call_user_func. See also How do I dynamically invoke a class method in PHP?

I think you'd like also to append another string to the callable functions like this:

public function homeAction(){}

in order to prevent a hacker to call methods that you probably don't want to be.

Community
  • 1
  • 1
s3v3n
  • 8,203
  • 5
  • 42
  • 56
  • Why not just make such methods private? – Ryan Jul 19 '11 at 17:26
  • Just in case you need to call that method from another class. Anyway, your comment question is subjective and this is the way ZendFramework does it actually, so I think there really is some logic in doing this. – s3v3n Jul 19 '11 at 17:28
  • Security through obscurity doesn't really count – Ryan Jul 19 '11 at 17:44
  • Your comment is pointless and groundless. I at least brought some arguments. – s3v3n Jul 19 '11 at 17:59