簡體   English   中英

PHP錯誤數組到字符串的轉換

[英]PHP Error Array to string conversion

我有具有路由屬性數組的類Router

<?php
class Router
{
    private $routes = [];

    function setRoutes(array $routes)
    {
        $this->$routes = $routes;
    }
}    
?>

在其他文件route.php中:

<?php    
$routes = [
    'users' => 'users.php',
    'comments' => 'comments.php'
];    
?>

我像這樣使用它:

<?php    
require __DIR__ .'/Data.php';
require __DIR__ .'/Router.php';
require __DIR__ .'/../routes.php';

$router = new Router;
$router->setRoutes($routes);
$url = $_SERVER['REQUEST_URI'];    
?>

但是我越來越

注意: /../XAMPP/xamppfiles/htdocs/TestPro/core/Router.php數組到字符串的轉換

為什么會發生此錯誤?

您應該替換以下行:

$this->$routes = $routes;

通過:

$this->routes = $routes;

屬性應在沒有$情況下調用

您的問題是在類中使用私有變量,您需要將類更改為:

class Router {

    private $routes = array();

    function setRoutes(array $routes){

$ this-> routes = $ routes;

    }

}

暫無
暫無

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

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