亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

PHP
青春有我 2022-09-12 12:52:44
當我在學校項目上工作時,我遇到了一個問題。我已經制作了一個路由器類,它可以獲得正確的文件,但我得到以下錯誤:致命錯誤: 方法 mvc\App::__toString() 不得引發異常,捕獲錯誤: 調用成員函數 getHTML() 字符串 /選擇/燈/htdocs/學校/測試PHP/mvc/公共/索引.php第 0 行echo 返回正確的文件路徑,因此這不是我遇到的問題。我認為問題是因為我嘗試將函數執行到字符串中。有誰知道我該如何解決我的問題?應用類:<?phpnamespace 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:<?phpnamespace 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<?phpdefine("DEFAULT_ROUTE", "home");$routes = array("home" => array(    "view" => "HomeView",    "controller" => "HomeController",),"form" => array(    "view" => "FormView",    "controller" => "FormController",),"test" => array(    "view" => "TestView",    "controller" => "TestController",),)?>測試視圖.php<?phpnamespace 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;}}?>
查看完整描述

1 回答

?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

問題:


好的,所以問題是你從字符串調用函數。


這是類中的方法:getViewRouter


public function getView(){

    return $this->view;

}

此方法返回一個字符串:


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

然后,您正在嘗試從以下字符串調用方法:


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

可能的解決方案:


顯然,您正在嘗試訪問類中的方法,因此在不了解有關您的設置的更多信息的情況下,很難獲得確切的信息,但是我會在下面進行猜測,您可以在注釋中指導我朝著正確的方向前進。getHTMLTestView


如果將類中的構造函數更改為: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();

我不確定你為什么會這樣做,然后,通常是一個或另一個,但如果你更詳細地闡述你預期的輸出,我可以幫你更多,或者希望我的解釋已經給了你足夠的工作。echoreturn


溶液:


閱讀注釋后,看起來要從方法的結果實例化類,可以通過將結果設置為變量并從該字符串調用新類,然后從該類調用該方法來執行此操作。getView


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

$class = new $class;


return $class->getHTML();


查看完整回答
反對 回復 2022-09-12
  • 1 回答
  • 0 關注
  • 98 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號