0

EDIT: According to the question: Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors? should work for me, but not. I probably don't understand something, but I can't see where the bug is. So, in my opinion, it`s not duplicate.

I'm a newbie in php so the question is probably simple and stupid. I have 3 files: index.php, View.php and layout.php. In View php I have a View class definition and a render () method in it. Among others, this method include html template from the file layout.php.

But: in the php file in the tag I have a piece of php code (if statement) which, depending on the value of the $ page variable (defined in the php file), includes a different file. The problem is that this variable appears as undefined in the layout.php file. I don't understand why, in the end all code is executed in index.php: View class is included and render function includes layout.php. Please help me understand this, because I can't do it. Thanks in advance

Notice: Undefined variable: action in C:\xampp\htdocs\notes\templates\layout.php on line 17

index.php

<?php

declare(strict_types=1);

namespace App;

require_once("src/utils/debug.php");
require_once("src/View.php");

$action = $_GET['action'] ?? null;

$view = new View();

$view -> render($action );

View.php

<?php

declare(strict_types=1);

namespace App;

class View
{
    public function render(?string $page): void
    {
        $apction = $page
        include_once("templates/layout.php");
    }
}

layot.php

<html>
<head>

</head>
<body>
<header>
    <h1>Nagłówek</h1>
</header>
<nav>
    <ul>
        <li><a href="index.php/?action=list">Lista notatek</li>
        <li><a href="index.php/?action=create">Nowa notatka</li>
    </ul>
</nav>
<main>
    <?php
    if ($action === 'create') {
    include_once("templates/pages/list.php");
    } else {
    include_once("templates/pages/create.php");
    }
     ?>

</main>
<footer>

</footer>
</body>
</html>
Michal
  • 3
  • 3
  • @ProfessorAbronsius Ok, but if I include layout.php inside render (), shouldn't the code execute inside the function I'm calling in index.php? This is an example from a php course and the tutor works fine and I don't quite understand why. Can you suggest a solution? Including index.php in layout.php doesn't seem like a solution – Michal Aug 19 '20 at 21:14
  • $action doesn't seem to have any value set. – Arundeep Chohan Aug 19 '20 at 21:24
  • `$apction = $page` .. typo? also missing a semicolon after. – IncredibleHat Aug 19 '20 at 21:26
  • @IncredibleHat Yeah,I lost the semicolon while struggling with the error. However, the error still says that the variable has not been declared, and according to what I read about the scope variable should be visible (the variable passed to the function and the file layout.php included in this function) – Michal Aug 19 '20 at 21:33
  • @arundeepchohan but it would be a loop: render() method including layout.php in to index.php, and in layout.php index.php is included too. However, i dont know how, but i fix it. PHP Storm still display error with "the variable has not been declared", but when i run it, all works. Does anyone know what this is about and can help me understand it? – Michal Aug 19 '20 at 22:00
  • I mean the view.php isn't really doing anything except passing values. – Arundeep Chohan Aug 19 '20 at 23:06
  • public function render($page) is also what the parameter should look like. – Arundeep Chohan Aug 19 '20 at 23:16

0 Answers0