【问题标题】:call php function name from a string [duplicate]从字符串调用php函数名[重复]
【发布时间】:2023-03-09 20:29:01
【问题描述】:

我创建了一个php函数来返回或保存一些json,类看起来像这样。

<?php
    class calendarModel {

        // global variables
        var $user;
        var $action;
        var $connect;

        // init class
        function __construct($action = "getEvents") {
            $this->user = 1;

            $this->action = $action;

            $dbhost = "localhost";
            $dbport = "5432";
            $dbname = "fixevents";
            $dbuser = "postgres";
            $dbpass = "123";
            $this->connect = pg_connect("host=" . $dbhost . " port=" . $dbport . " dbname=" . $dbname . " user=" . $dbuser . " password=" . $dbpass);

            $this->executeAction();
        }

        // action router
        function executeAction() {
            if($this->action == "getEvents")
                $this->getEvents();
            else if($this->action == "moveEvent")
                $this->moveEvent();
            else if($this->action == "insertEvent")
                $this->insertEvent();
            else if($this->action == "updateEvent")
                $this->updateEvent();
            else if($this->action == "getCalendars")
                $this->getCalendars();
            else if($this->action == "toggleCalendar")
                $this->toggleCalendar();
            else if($this->action == "deleteCalendar")
                $this->deleteCalendar();
            else if($this->action == "insertCalendar")
                $this->insertCalendar();
        }

        // getEvents
        function getEvents() {
            //...
        }

        // moveEvent
        function moveEvent() {
            //...
        }

        // insertEvent
        function insertEvent() {
            //...
        }

        // updateEvent
        function updateEvent() {
            //...
        }

        // toggleCalendar
        function toggleCalendar() {
            //...
        }

        // deleteCalendar
        function deleteCalendar() {
            //...
        }

        // insertCalendar
        function insertCalendar() {
            //...
        }

    }

    // call class
    if(isset($_GET['action']))
        $instance = new calendarModel($_GET['action']);
    else
        $instance = new calendarModel();
?>

我想知道的是,我能否以某种方式从字符串名称调用构造中的操作,而不是使调用 executeAction 的函数变大 if / else if 函数。提前谢谢你,丹尼尔!

【问题讨论】:

  • @zerkms $this 会使事情复杂化。
  • @kapa: call_user_func(array($this, 'methodname'))
  • @zerkms 嗯,好久没用 PHP 了。我以为你只能在数组中传递字符串。感谢您清理它。
  • 我试过 call_user_func($action);和 $action();但他们不工作
  • @Pacuraru Daniel:你可能在使用之前阅读过函数文档。

标签: php


【解决方案1】:

如果你使用一个将使用函数名的表达式,则表达式的值将用作函数名:

function executeAction() {
    $this->{$this->action}();
}

由于您是从用户输入中获取操作的,因此请确保对其进行验证。否则,有人可能会发送使您执行任意方法的输入。

【讨论】:

  • 非常感谢您按预期工作:我希望我能给你们两个正确的答案:)
【解决方案2】:

Barmar 几乎是正确的: $this->{$action}();

【讨论】:

  • 非常感谢,这很完美,我什至不再需要执行执行动作功能,我只是在结构中这样调用它
  • 这个答案不值得被检查,对不起
  • $action是类属性,需要$this-&gt;
  • 其实我就是这样用的,它对我有用,可能是因为我在contructor中使用它
  • ops,这是我的注意力不足:/ Barmar 是正确的 :)
【解决方案3】:

使用类似的东西:

function __construct($action = "getEvents")
{
    ...
    $this->$action();
}

由于 $action 是由用户定义的,您可能需要检查 $action 是否是您的类中的现有函数:

if (array_key_exists($action, get_class_methods($this))) {
    $this->$action();
}

【讨论】:

    猜你喜欢
    • 2011-12-17
    • 1970-01-01
    • 2013-01-03
    • 2011-05-14
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2017-07-02
    相关资源
    最近更新 更多