0

When I go to my site, it is supposed to redirect me to google.com

Here is my structure:

application
    -config
        -Application.php
        -Router.php
public
    -index.php

Here is my code:

index.php:

require '../application/config/Application.php';
$app = new Application();

Application.php:

<?php
require dirname(__DIR__).'config/Router.php';

class Application {

    private $router = new Router();

    public function __construct() {
        $this->router;
    }

}
?>

Router.php;

class Router {

    public function __construct() {
        header('Location: https://www.google.com');
    }

}

I want it to redirect from index -> to Application.php -> to Router.php -> redirect to google.com in construct.

EDIT: I know it gets to Application.php because if I put the header in the Application.php construct, it redirects. it's just not getting to the Router.php

Why am i doing all of this? because I want to test if everything is working first.

tereško
  • 58,060
  • 25
  • 98
  • 150
baileyJchoi
  • 473
  • 5
  • 17
  • 2
    Any error messages showing up? You said it wasn't working, but didn't describe what was happening. – Mike Nov 14 '16 at 18:26
  • @mike it does not redirect to google.com – baileyJchoi Nov 14 '16 at 18:28
  • what have you tried to do, to debug the problem? did you get any error messages? did you try checking if the code is executed? (if it fails at executing, where does it fail???) Try echoing stuff out just to check if the desired code is ever reached by the server –  Nov 14 '16 at 18:29
  • @Hallur where should I start echo'ing? I know it gets to Application.php because if I put the `header` in the Application.php construct, it redirects. it's just not getting to the Router.php – baileyJchoi Nov 14 '16 at 18:30
  • outside the class in application.php try echoing out "application.php", if the site echoes it out, then the file was included, if it doesn't, then the file wasn't included... if it is included, try echoing "test" or something out inside the function where you redirect... you have to check if the code is run... just because nothing happens, that doesn't mean the code doesn't work, it could mean that the code is simply not reached. –  Nov 14 '16 at 18:32
  • @Hallur ok, it worked if I put the echo before the `require` but does not work if I put it right after the `require` – baileyJchoi Nov 14 '16 at 18:35
  • @baileyJchoi Enable error reporting and check your error logs. That would have told you what the problem was, which will be (as dont-panic's answer alludes to) that the file you're trying to require, isn't available / doesn't exist. – Jonnix Nov 14 '16 at 18:36

1 Answers1

4

private $router = new Router(); isn't going to work. You'll need to assign it in the constructor instead.

class Application {

    private $router;

    public function __construct() {
        $this->router = new Router();
    }

}

From the PHP documenation on properties:

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

new Router() is not able to be evaluated at compile time.


Also, require dirname(__DIR__).'config/Router.php'; looks like it will cause you problems, as dirname(__DIR__) won't have a trailing slash.

You would be a lot better off with this sort of debugging if you enabled error reporting. The errors do a pretty good job of telling you where the problems lie, and without them your debugging will just be doing a lot of shooting in the dark.

Community
  • 1
  • 1
Don't Panic
  • 41,125
  • 10
  • 61
  • 80