Andychan 发表于 2011-6-18 15:12:45

自訂類庫失敗了

本帖最后由 Andychan 于 2011-6-18 15:13 编辑

application/libraries/LoadData.php


<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class LoadData {

    function __construct()
    {   
      $a = "test";   
    }
}

?>

application/controllers/about.php

<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class About extends CI_Controller {

    public function index(){   
      $this->load->library('LoadData');
      $data['a'] = $a;
      
      // layout part
      $this->load->view('header',$data);
      $this->load->view('about_view');
      $this->load->view('footer');
    }
}

/* End of file about.php */

application/views/about_view.php

      <div class="right">
      
            About Me :) <bt />
            <?=$a;?>
            
      </div>
   
    <div class="clear"></div><!-- end clear -->
   
    </div><!-- end main -->

Show Error Message
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: a

Filename: controllers/about.php

Line Number: 13

jeongee 发表于 2011-6-18 15:37:11

首先文件命名方式不对,其次你那样也得不到构造函数中a变量的值
仔细研读下手册

Andychan 发表于 2011-6-18 16:49:24

那請問一下, public function index 的 public 是否多餘的了?
剛看手冊的方法是不寫 Public

visvoy 发表于 2011-6-18 17:31:11

不写是兼容php4

Andychan 发表于 2011-6-18 17:48:30

前輩的意思, 那如果我用 php5 其實是可有可無, 對吧
页: [1]
查看完整版本: 自訂類庫失敗了