我有类Router有路由属性的类
<?php
class Router
{
private $routes = [];
function setRoutes(array $routes)
{
$this->$routes = $routes;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
在其他文件routes.php中:
<?php
$routes = [
'users' => 'users.php',
'comments' => 'comments.php'
];
?>
Run Code Online (Sandbox Code Playgroud)
我用它像:
<?php
require __DIR__ .'/Data.php';
require __DIR__ .'/Router.php';
require __DIR__ .'/../routes.php';
$router = new Router;
$router->setRoutes($routes);
$url = $_SERVER['REQUEST_URI'];
?>
Run Code Online (Sandbox Code Playgroud)
但我得到了
注意:数组转换为字符串 /../XAMPP/xamppfiles/htdocs/TestPro/core/Router.php
为什么会出现这种错误?
你应该替换这一行:
$this->$routes = $routes;
Run Code Online (Sandbox Code Playgroud)
通过:
$this->routes = $routes;
Run Code Online (Sandbox Code Playgroud)
应该在没有的情况下调用属性$