使用未定义的常量 - 学习OOP PHP

Red*_*ent 1 php oop

未定义的常量问题不是一个新的问题,我已经尝试了谷歌,发现了一百万个答案,涉及缺少引号的数组,这不是我的解决方案.我正在学习使用PHP进行OOP编码,这实际上是我使用它的前几个小时.这是我创建的课程.

class template {

var $page;

function __construct() {
    $this->page = 'home';
}

function getHeader($header = 'header') {
    include ('template/'.$header.'.php');
}

function getFooter($footer = 'footer') {
    include ('template/'.$footer.'.php');
}

function setPage($page = 'home') {
    $this->page = $page;
}

function getPage() {
    if(page === 'home') {
        include('template/home.php');
    } else {
        include('template/'.$this->page.'.php');
    }
}



}
Run Code Online (Sandbox Code Playgroud)

这就是我实例化它的方式.

include('class.template.php'); 

$template = new template();

$template->getHeader();

if(isset($_GET['page'])) {
    $template->setPage($_GET['page']);
}

$template->getPage();



$template->getFooter();
Run Code Online (Sandbox Code Playgroud)

当然还有错误 - 注意:使用未定义的常量页面 - 在第24行的/Applications/MAMP/htdocs/titan-up/class.template.php中假定为'page'

这显然是我应该立刻发现我做错了但现在已经很晚了,我不知所措.任何帮助是极大的赞赏.

编辑:非常感谢任何使学习过程更容易的链接.我已经阅读了PHP手册,并且拥有PHP的强大背景,但不是OOP.

Joh*_*ter 8

改变一下:

function getPage() {
    if(page === 'home') {
Run Code Online (Sandbox Code Playgroud)

对此:

function getPage() {
    if($this->page === 'home') {
Run Code Online (Sandbox Code Playgroud)

错误消息"注意:使用未定义的常量页面 - 假设'页''并不是非常有用,但是由于不幸的事实,PHP将隐式地将未知标记转换为具有相同值的常量.

就是它正在看到page(它没有$在它前面,因此不是一个变量名),并将它视为先前的声明define('page', 'page');.

注意:此错误消息的另一个常见原因是,如果您忘记将字符串包装在引号中 - 例如,$some_array[some_key]而不是$some_array['some_key'].