【问题标题】:How do I execute a function on a string PHP如何在字符串 PHP 上执行函数
【发布时间】:2020-05-23 12:36:02
【问题描述】:

当我在做一个学校项目时,我遇到了一个问题。

我创建了一个路由器类,它可以在其中获取正确的文件,但出现以下错误:

致命错误:方法 mvc\App::__toString() 不得抛出异常,已捕获错误:调用 /opt/lampp/htdocs/School/testPHP/mvc/public/ 中字符串上的成员函数 getHTML()第 0 行的 index.php

回显返回正确的文件路径,所以这不是我遇到的问题。

我认为问题在于我尝试将函数执行到字符串中。

有人知道如何解决我的问题吗?

应用类:

<?php
namespace mvc;

class App{
    private $router;

    public function __construct(){
        $this->router = new \mvc\Router();
    }


    public function __toString(){
        try {
            echo $this->router->getView(); //this returns the correct file path 
            return $this->router->getView()->getHTML();
        } catch (Exception $e) {
            return $e.getMessage;
        }
    }
}
?>

Router.php:

<?php
namespace mvc;

class Router{

private $route;
private $view;
private $controller;
private $model;

public function __construct(){
    require_once(LOCAL_ROOT. "php/Routes.php");
    if (isset($_GET['route'])){
        $this->route = explode("/" , $_GET['route']);
    }
    $route = isset($routes[$this->getRoute()])? $this->getRoute() : DEFAULT_ROUTE;
    $this->controller = "\\controllers\\". $routes[$route]['controller'];
    $this->view = "\\views\\". $routes[$route]['view'];
    // $model = "\\models\\". $routes[$route]['model'];
}

private function getRoute(){
    return count($this->route) > 0 ? $this->route[0] : DEFAULT_ROUTE;
}
public function getView(){
    return $this->view;
}
}
?>

Routes.php

<?php
define("DEFAULT_ROUTE", "home");
$routes = array(
"home" => array(
    "view" => "HomeView",
    "controller" => "HomeController",
),
"form" => array(
    "view" => "FormView",
    "controller" => "FormController",
),
"test" => array(
    "view" => "TestView",
    "controller" => "TestController",
),
)
?>

TestView.php

<?php
namespace views;

class TestView extends \mvc\View{
public function getHTML(){
    // return 'dit is testView';
    $klik = $this->controller->getGetData("klik");
    $output = "";
    $output .= "<h1>".$klik++ ."</h1>";
    $output .= "<a href=\"test?klik=$klik\">klik</a>";
    $output .= "<br>";
    return $output;
}
}

?>

【问题讨论】:

  • 你不能从 __toString() 方法抛出异常。你可以在 PHP 版本 PHP 7.4 中做到这一点。
  • 我们需要查看Router类的代码,特别是getView()getHTML()方法。
  • @MarkOverton 我已经添加了一些代码希望它是你需要的
  • 简短回答:您不能像在 JavaScript 中那样在 PHP 字符串上调用方法,因为它们是原语。您可以将字符串作为参数传递给函数。
  • return $e.getMessage; 在你的 App 类中没有做你想做的事;正确的语法是return $e-&gt;getMessage();。虽然这不能解决根本原因,但解决这个问题听起来是不错的第一步。

标签: php


【解决方案1】:

问题:

好的,问题是您正在从字符串调用函数。

这是Router 类中的getView 方法:

public function getView(){
    return $this->view;
}

这个方法返回一个字符串:

$this->view = "\\views\\". $routes[$route]['view'];

然后你试图从这个字符串调用一个方法:

return $this->router->getView()->getHTML();

潜在的解决方案:

显然您正在尝试访问 TestView 类中的 getHTML 方法,因此在不了解您的设置的情况下,很难获得准确信息,但我会猜测以下内容,您可以指导我在 cmets 中的方向是正确的。

如果您将 App 类中的构造函数更改为:

public function __construct(){
    $this->router = new \mvc\Router();
    $this->testView = new \views\TestView();
}

然后您可以将__toString 方法更改为以下内容:

echo $this->router->getView(); //this returns the correct file path 
return $this->testView->getHTML();

我不知道你为什么要先echo 然后return,通常是其中一个,但如果你详细说明你的预期输出,我可以帮助你更多,或者希望我的解释已经给出你足够工作了。


解决方案:

阅读完您的 cmets 后,您似乎想从 getView 方法的结果中实例化该类,您可以通过将结果设置为一个变量并从该字符串调用一个新类然后调用该类的方法。

$class = $this->router->getView();
$class = new $class;

return $class->getHTML();

【讨论】:

  • 是的,这会起作用,但我的问题是我制作了我的程序,以便它会选择我的 url 的最后一部分并使用它找到合适的类 FE:在 testPHP/mvc/test the echo $this->router->getView();将返回 \views\TestView 并在 estPHP/mvc/home 返回 echo $this->router->getView();将返回 \views\HomeView
  • 你希望return $this-&gt;router-&gt;getView()-&gt;getHTML();做什么?
  • 它需要获取正确的视图类,然后在该视图类上执行函数getHTML()
  • 请参阅上面的修订答案以了解如何执行此操作
猜你喜欢
  • 2012-07-29
  • 1970-01-01
  • 2021-03-16
  • 1970-01-01
  • 2016-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多