2012-07-10 83 views
-1

我是CodeIgniter中的完整初學者。我試圖理解MVC模式,因爲我將繼續使用CodeIgniter,這會變得更加棘手。 這是我的控制器看起來像即hello.php:在控制器和視圖之間創建和發送參數

<?php 
    class hello extends CI_Controller 
    { 
     var $name; 
     var $color; 
     function hello() 
     { 
      parent::Controller(); 
      $this->name ='Leroy'; 
      $this->color ='red'; 
     } 

     function show() 
     { 
      $data['name'] =$this->name; 
      $data['color']=$this->color;  
      $this->load->view('show_message',$data); 
     } 
    } 
?> 

的觀點,即show_message.php

<p align="center">Hello <font color="<?=$color?>"><?=$name?></font>..!!!!.</p> 

當我運行該腳本它給這個錯誤

Fatal error: Call to undefined method CI_Controller::Controller() in C:\xampp\htdocs\CodeIgniter\application\controllers\hello.php on line 8 

PS我使用CodeIgniter版本2.0,因此我將類名更改爲CI_Controller

+0

替換構造函數的代碼? – Hardik 2012-07-10 10:34:03

+0

@hardik php 5.3 – 2012-07-10 10:34:49

+1

http://codeigniter.com/user_guide/general/controllers.html請參閱指南的Class Constructors部分,代碼的構造函數部分針對的是舊版的php。對於新的PHP你必須使用__construct方法 – Hardik 2012-07-10 10:36:28

回答

1
function hello() 
    { 
     parent::Controller(); 
     $this->name ='Leroy'; 
     $this->color ='red'; 
    } 

與此

function __construct() 
    { 
     parent::__construct(); 
     $this->name ='Leroy'; 
     $this->color ='red'; 
    } 
您使用PHP4或PHP5
+0

它沒關係我給任何函數名....我想'function __construct()'不是強制性的。 – 2012-07-10 11:24:36

+0

是的,它不是強制性的。但在Php的標準 – 2012-07-10 11:27:46

1
function hello() 
     { 
      parent::__construct(); 
     } 
+0

那確實解決了problem.but你能解釋你爲什麼使用that.??。現在我沒有得到我想要的輸出它應該顯示'你好我的名字'它剛剛顯示'你好' – 2012-07-10 10:37:59

+1

1. http://php.net/manual/en /language.oop5.decon.php 2.嘗試使用<?php echo $ name?> – Samson 2012-07-10 10:41:38

+1

yea。短標籤可能是問題。它在默認情況下關閉wamp版本 – 2012-07-10 11:05:08

相關問題