General problem with code running more than once

I like to run my code just once. But somehow it runs twice, which causes some further problems.


The Problem

The method setCurrentItem on Page runs twice.

The value true is dumped two times by var_dump(true).

I would like to implement a conditional redirect which does not work because the code is run more than once.

I would also like to get some general coding advice on my code, because I am relatively new to PHP.

Thanks, everyone!


Now: The Code

index.php:

<?php

require_once '../src/App.php';
require_once '../src/Page.php';
require_once '../src/Item.php';

$app = new App();

App.php:

<?php

class App
{
    public $pages = [];
    public $currentPage = null;

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

    private function loadPages()
    {
        $paths = glob('../content/**/page.json');

        foreach ($paths as $path) {
            $data = json_decode(file_get_contents($path));
            $this->pages[$data->slug] = new Page($data);
        }
    }

    private function setCurrentPage()
    {
        $pathSegments = isset($_GET['path']) ? explode('/', trim($_GET['path'], '/')) : [];
        $slug = isset($pathSegments[0]) ? $pathSegments[0] : 'home';

        if (array_key_exists($slug, $this->pages)) {
            $this->currentPage = $this->pages[$slug];
        } else {
            $siteConfig = json_decode(file_get_contents('../config/site.json'));
            header('Location: '.$siteConfig->basePath);
        }
    }
}

Page.php:

<?php

class Page
{
    public $slug;
    public $title;
    public $dynamic;

    public $items = [];
    public $currentItem = null;

    public function __construct($data)
    {
        $this->distributeData($data);

        if ($this->dynamic) {
            $this->loadItems();
            $this->setCurrentItem();
        }
    }

    private function distributeData($data)
    {
        $this->slug = $data->slug;
        $this->title = $data->title;
        $this->dynamic = $data->dynamic;
    }

    private function loadItems()
    {
        $paths = glob('../content/*.'.$this->slug.'/**/item.json');
        
        foreach ($paths as $path) {
            $data = json_decode(file_get_contents($path));
            $this->items[$data->slug] = new Item($data);
        }
    }

    private function setCurrentItem()
    {
        $pathSegments = isset($_GET['path']) ? explode('/', trim($_GET['path'], '/')) : [];
        $slug = isset($pathSegments[1]) ? $pathSegments[1] : null;

        if (array_key_exists($slug, $this->items)) {
            $this->currentItem = $this->items[$slug];
        }

        var_dump(true);
    }
}