用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2102|回复: 4
收起左侧

[HELP] 自訂類庫失敗了

[复制链接]
发表于 2011-6-18 15:12:45 | 显示全部楼层 |阅读模式
本帖最后由 Andychan 于 2011-6-18 15:13 编辑

application/libraries/LoadData.php

PHP复制代码
 
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 
class LoadData {
 
    function __construct()
    {    
        $a = "test";    
    }
}
 
?>
复制代码


application/controllers/about.php

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
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

发表于 2011-6-18 15:37:11 | 显示全部楼层
首先文件命名方式不对,其次你那样也得不到构造函数中a变量的值
仔细研读下手册
 楼主| 发表于 2011-6-18 16:49:24 | 显示全部楼层
那請問一下, public function index 的 public 是否多餘的了?
剛看手冊的方法是不寫 Public
发表于 2011-6-18 17:31:11 | 显示全部楼层
不写是兼容php4
 楼主| 发表于 2011-6-18 17:48:30 | 显示全部楼层
前輩的意思, 那如果我用 php5 其實是可有可無, 對吧

本版积分规则