2

I have class Router which has array of routes property

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

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

In other File routes.php :

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

And I use it like :

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

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

But I'm getting

Notice: Array to string conversion in /../XAMPP/xamppfiles/htdocs/TestPro/core/Router.php

Why this error is occurring ?

1
  • 1
    Is there any more code in the file that contains the Router class? Can you give us the content of the line of code within Router.php that creates the notice? Commented Nov 13, 2017 at 16:37

2 Answers 2

6

YOu should replace this line:

$this->$routes = $routes;

By:

$this->routes = $routes;

Attributes should be called without $

Sign up to request clarification or add additional context in comments.

Comments

1

You problem is the use of private variable in the class, you need change the class for:

class Router {

    private $routes = array();

    function setRoutes(array $routes){

$this->routes = $routes;

    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.