簡體   English   中英

如何在字符串 PHP 上執行函數

[英]How do I execute a function on a string PHP

當我在做一個學校項目時,我遇到了一個問題。

我已經創建了一個 Router 類,它可以獲取正確的文件,但是出現以下錯誤:

致命錯誤:方法 mvc\\App::__toString() 不得拋出異常,已捕獲錯誤:在 /opt/lampp/htdocs/School/testPHP/mvc/public/index.php 中的字符串上調用成員函數 getHTML()在第 0 行

echo 返回正確的文件路徑,所以這不是我遇到的問題。

我認為問題是因為我嘗試將函數執行到字符串中。

有誰知道我該如何解決我的問題?

應用類:

<?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;
        }
    }
}
?>

路由器.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;
}
}
?>

路由.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",
),
)
?>

測試視圖.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;
}
}

?>

問題:

好的,問題是您正在從字符串調用函數。

這是Router類中的getView方法:

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

此方法返回一個字符串:

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

然后,您嘗試從此字符串調用方法:

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

潛在的解決方案:

顯然,您正在嘗試訪問TestView類中的getHTML方法,因此在不了解更多設置的情況下,很難獲得准確的信息,但我會猜測以下內容,您可以指導我朝着正確的方向前進注釋。

如果您將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 ,通常是一個或另一個,但是如果你詳細說明你的預期輸出,我可以幫助你更多,或者希望我的解釋已經給了你足夠的工作。


解決方案:

閱讀您的評論后,您似乎想從getView方法的結果實例化該類,您可以通過將結果設置為變量並從該字符串調用新類然后從該類調用該方法來實現此目的。

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

return $class->getHTML();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM