附带的PHP文件中变量的可见性问题

问题描述

编辑: 根据问题:Reference: What is variable scope,which variables are accessible from where and what are “undefined variable” errors? 应该为我工作,但不是。我可能不了解某些内容,但看不到错误在哪里。因此,我认为这不是重复的。

我是php的新手,因此问题可能很简单又很愚蠢。我有3个文件:index.php,View.php和layout.php。在View php中,我有一个View类定义和一个render()方法。除其他外,此方法包括来自layout.php文件中的html模板。

但是:在php文件的标记中,我有一段php代码(if语句),根据$ page变量的值(在php文件中定义),它包含一个不同的文件。 问题在于该变量在layout.php文件中显示为未定义。 我不明白为什么,最终所有代码都在index.php中执行:包括View类,而render函数包括layout.php。 请帮助我理解这一点,因为我做不到。预先感谢

注意:未定义的变量:第17行在C:\ xampp \ htdocs \ notes \ templates \ layout.php中的动作

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>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...