总是被调用的 PHP 方法

发布于 2024-10-09 00:06:15 字数 437 浏览 1 评论 0原文

我目前正在开发自己的 PHP-MVC 框架(仅用于体验目的)。

我的问题:是否可以在每次类方法时调用定义的函数或方法 已被调用?

例如:

public function view($id) {
     //Code ...
     $this->view->render(__FUNCTION__);
}

我想要的是:

public function view($id) {
    //Code ...
    //render-method is called automatically with functionname as parameter
}

我尝试了不同的方法......但没有成功。 如果有人能帮助我解决这个问题那就太好了。

干杯, 克里斯

I'm currently working on an own PHP-MVC-Framework (for experience purposes only).

My question: Is it possible to call a defined function or method, every time a class-method
has been called?

For example:

public function view($id) {
     //Code ...
     $this->view->render(__FUNCTION__);
}

What I want is:

public function view($id) {
    //Code ...
    //render-method is called automatically with functionname as parameter
}

I tried different methods ... but without success.
Would be great if someone could help me out with this.

Cheers,
Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不一样的天空 2024-10-16 00:06:15

您可以使用 MagicMethods 来实现此行为:

public function __call($func, $args) {
  if(!method_exists($this, $func)) {
    return;
  }

  // do some coding here
  call_user_func_array($func,$args);
  // do some coding there
}

private function view($arg1, $arg2) {
  // and here
}

记住:视图函数必须是私有的/受保护的。

$obj->view("asdasd", "asdsad");

应该执行 ::__call(),然后执行 ::view() 方法

You can use Magic Methods do achieve this behavior:

public function __call($func, $args) {
  if(!method_exists($this, $func)) {
    return;
  }

  // do some coding here
  call_user_func_array($func,$args);
  // do some coding there
}

private function view($arg1, $arg2) {
  // and here
}

Remember: view function must be private/protected.

$obj->view("asdasd", "asdsad");

Should do ::__call(), then ::view() method

最美的太阳 2024-10-16 00:06:15

您可以使用 PHP 使用变量值来执行的能力来创建一个函数作为联络人。例如:

function call($func,$param)
{
    $this->$func($param);
    $this->render($func);
}

$myObj->call('view',$id);

You could create a function as a liaison using PHP's ability to use variable values for execution purposes. for example:

function call($func,$param)
{
    $this->$func($param);
    $this->render($func);
}

$myObj->call('view',$id);
任谁 2024-10-16 00:06:15

您可以使用包装方法。调用此方法并将其他所有内容作为参数传递。

You can use a wrapper method. Call this method and pass everything else as a parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文