TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Action overloading in Kohana?
  • Hi, I'm trying to overload an action, so that if the user calls it with arguments it should show something, but when it is called without arguments it should show something else.

    What I get though is:

    Fatal error: Class declarations may not be nested in /usr/www/users/mysite/system/classes/html.php on line 3

    <?php defined('SYSPATH') or die('No direct script access.');<br />
    class Controller_User extends Controller_DefaultTemplate {

    public function action_activate()
    {
    $this->template->content = 'No arguments';
    }

    public function action_activate($arg)
    {
    $this->template->content = $arg;
    }
    }

    Is this impossible? Thanks!
  • That's what default arguments are for. If the user doesn't put an argument, the default is used. If he puts an argument, the default is overridden. No?

    You can do something like this:


    public function action_activate()
    {
    $arg = $this->request->param('arg');
    empty($arg) AND $arg = 'no arguments';

    $this->template->content = $arg;
    }
  • IIRC multiple methods arn't supported by PHP yet.

    From the php manual:

    PHP's interpretation of "overloading" is different than most object oriented languages. Overloading traditionally provides the ability to have multiple methods with the same name but different quantities and types of arguments.



    An addition to xenakis:


    public function action_activate()
    {
    $arg = $this->request->param('arg', 'no arguments');

    $this->template->content = $arg;
    }
  • @dyron - sweet you can pass a default in there. Thanks for the tip.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion