php示例的错误记录

最近几天在测试php的mvc,从网上找到几个示例。

先学习这一篇,http://www.cnblogs.com/q1ng/p/4529496.html

标题是  PHP的MVC框架 深入解析,其实是最简单的,感谢Q1NG的代码。

有了第一篇的基础,学习第二篇,http://www.cnblogs.com/foonsun/p/5788564.html

标题是 php简单实现MVC,和第一篇差不多,内容更进一步了。却叫简单实现,这位仁兄挺谦虚。感谢乔晓峰的代码。

第一篇没错,很顺利,第二篇有错误。

第二篇,视图View这一节课中,参数错误,无法打开页面

源代码无法执行,会提示定义错误,Notice: Undefined variable:

 // controller/democontroller.php
 class DemoController
 {
     private $data = 'Hello furzoom!';
     public function index()
     {
     //echo 'hello world';
     require('view/index.php');
     $view = new Index();
     $view->display($data);
     }
 }// End of the class DemoController
 // End of file democontroller.php

改成

 // controller/democontroller.php
 class DemoController
 {
     private $data = 'Hello furzoom!';
     public function index()
     {
     //echo 'hello world';
     require('view/index.php');
     $view = new Index();
     $view->display($this->data);
     }
 }// End of the class DemoController
 // End of file democontroller.php

注意,$view->display($data);改成$view->display($this->data);

顺手记一下,做类的构造函数时,使用旧式写法比较安全些。

下面这个代码是另一篇教程中的,无法执行,代码错误,

地址 http://www.cnblogs.com/soundcode/p/6903826.html   感谢左正的代码。

class test
{ var $b;
function test() { $this->b=5; }
function addab($c) { return $this->b+$c; }
}
$a = new test(); echo $a->addab(4); // 返回 9

改成下面这样就可以了。

class test
{ var $b;
function __construct() { $this->b=5; }
function addab($c) { return $this->b+$c; }
}
$a = new test(); echo $a->addab(4); // 返回 9

代码风格就是程序员的脸面,要把生命中所有的才华、心血都倾注在code的字里行间。
原文地址:https://www.cnblogs.com/bhss/p/7345432.html