我正在创建一个像MVC这样的系统。
当我想调用视图文件时,我正在使用下面的类。编辑:同时,他可以做到这一点:http://www.youtube.com/watch?feature=player_detailpage&v=Aw28-krO7ZM#t=1166s
<?php
class Call{
function __construct($fileName) {
//$this->db = new Database;
self::callFile($fileName);
}
function callFile($fileName)
{
$this->title = "example";
$this->description = "example";
$this->keywords = "example";
$fileName = $fileName . '.php';
require PAGESPATH.'common/header.php';
require PAGESPATH.$fileName;
require PAGESPATH.'common/footer.php';
}
}
?>
$fileName是索引.php。索引.php只有
Index Page..
我想在标题中打印数据.php如下所示:
<html>
<head>
<meta charset="utf-8">
<title><?php if(isset($this->title)){echo $this->title;} ?> - WebProgramlama.tk</title>
<meta name="description" content="<?php if(isset($this->description)){echo $this->description;}?>" />
<meta name="keywords" content="<?php if(isset($this->keywords)){echo $this->keywords;} ?>" />
</head>
<body>
但是我遇到了错误。
Fatal error: Using $this when not in object context in /var/www/webprogramlama/class/pages/common/header.php on line 4
我能解决这个问题吗?注意:请小心!标头.php 由 Call 类调用。标头.php 位于 Call 类内部。
编辑:为什么会运行?
别再看那个愚蠢的"教程"了。这太糟糕了,以至于我一度真的开始笑了。
你不能只是在没有任何经验的情况下学习一门语言,然后开始使用高级概念。MVC就是这样的概念之一。要真正掌握它,你需要了解面向对象的编程和许多与之相关的原则。
- 单一责任原则
- 关注点分离
- 得墨忒耳定律
- 控制反转
- 利斯科夫替代原理
。。等。而且您不会仅通过阅读文章来理解这些原则。
至于如何解决您的问题,您可以阅读本文。它将解释如何使用模板。这实际上是您的"mvc 教程"实际上是什么 - 制作路由机制和模板的糟糕指南。
另外,您必须了解,如果您确实self::something();
,则它在对象之外。您正在类上调用静态方法,这实际上只是进行过程编程的糟糕方法。
你应该从学习 PHP 中 OOP 的基础知识开始,因为你不明白它。为了你自己的利益,至少一年远离MVC和框架。
在代码中创建对象的新实例,而不是在类外部调用$this->title
元素,如下所示:
$callObject= new call($filename);
然后在页面中像这样引用它:
$callObject->title;
您只能在类本身中使用$this->
代码。在其他任何地方,你需要创建一个对象,因此$this
不存在 - 该类的对象存在 - 然后你必须通过它的名称引用它。同时,如果你的变量要被调用$callObject
你不能在类中像那样引用它 - 因为那时你还没有创建它的实例,所以你需要通过$this
语法引用它,这是说my element called title
的好方法。
编辑:好的,我知道你现在在做什么,天哪。
这是相当危险的,因为你的头文件.php文件将包含大量只能在称为类的类中工作的东西 - 并在所有其他情况下产生可怕的错误。
PHP会让你拥有header.php
文件,但是当 PHP 评估内容时,你的对象是不完整的。您应该阅读此问题,它将更详细地回答该部分。
编辑 2:不要在文件之间拆分函数。
如果编写header.php
中的代码仅在函数中使用(看起来如此),请在类中复制它的全部内容 - 并且不要使用require
或include
来尝试动态粘贴它。
一个类永远不要有多个文件。文件有多长并不重要。
编辑3:
这是标题部分的代码应该是什么样子:
<?php
class Call{
function __construct($fileName) {
//$this->db = new Database;
self::callFile($fileName);
}
function callFile($fileName)
{
$this->title = "example";
$this->description = "example";
$this->keywords = "example";
$fileName = $fileName . '.php';
echo "
<html>
<head>
<meta charset='utf-8'>
<title>
";
echo (isset($this->title)) ? $this->title : "";
echo "
- WebProgramlama.tk</title>
<meta name='description' content='
";
echo (isset($this->description)) ? $this->description : "";
echo "' />
<meta name='keywords' content='
";
echo (isset($this->contents)) ? $this->contents : "";
echo "
' />
</head>
<body>
";
//require PAGESPATH.$fileName;
//require PAGESPATH.'common/footer.php';
// you can only include files that don't use any $this-> type elements.
}
}
?>
$this
在类之外没有上下文。 创建类的实例并改用该实例。
<html>
<head>
<meta charset="utf-8">
<?php
$class = new Call('filename');
?>
<title><?php if(isset($class->title)){echo $class->title;} ?> - WebProgramlama.tk</title>
<meta name="description" content="<?php if(isset($class->description)){echo $class->description;}?>" />
<meta name="keywords" content="<?php if(isset($class->keywords)){echo $class->keywords;} ?>" />
</head>
<body>
去掉callFile
函数中的require
语句;它们没有位置。