phoenixg 发表于 2011-7-24 13:20:03

着急,有关自定义辅助函数的问题

article.php 控制器

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

class Article extends CI_Controller {

    function __construct()
    {
      parent::__construct();
      $this->load->helper('MY_others');
      is_logged_in('Article');
    }


MY_others_helper.php 自定义辅助函数

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

if ( ! function_exists('is_logged_in'))
{
    function is_logged_in($object)
    {
      $is_logged_in = $object->session->userdata('is_logged_in');
      if(!isset($is_logged_in) || $is_logged_in != true)
      {
            echo 'You don\'t have permission to access this page.&nbsp;';
            echo anchor('member/index','Login');   
            die();   
      }   
    }
}


报错:
A PHP Error was encounteredSeverity: Notice
Message:Trying to get property of non-object
Filename: helpers/MY_others_helper.php
Line Number: 19

( ! ) Fatal error: Call to a member function userdata() on a non-object in E:\xampp\htdocs\ci_volunteers\app\helpers\MY_others_helper.php on line 19

应该怎么改?

jeongee 发表于 2011-7-24 13:23:35

文件命名问题。
你看下system/helpers/下的文件有大写字母开头的吗?
另外HELPER的不需要加MY_这个前缀。

phoenixg 发表于 2011-7-24 13:25:28

jeongee 发表于 2011-7-24 13:23 static/image/common/back.gif
文件命名问题。
你看下system/helpers/下的文件有大写字母开头的吗?
另外HELPER的不需要加MY_这个前缀。 ...

是放在application/helpers下的,应该不是文件命名问题,因为该辅助函数里的另一个自定义函数是起作用的。

jeongee 发表于 2011-7-24 13:29:47

phoenixg 发表于 2011-7-24 13:25 static/image/common/back.gif
是放在application/helpers下的,应该不是文件命名问题,因为该辅助函数里的另一个自定义函数是起作用的 ...

哦,是你传进去的object变量有问题,你传的是类名的字符串。。。。怎么会有session。
让你的$object = & get_instance();吧
PS:你的文件命名会有问题的,你可能现在用的windows,它不区分大小写,如果你传到linux等下,就会报错了

phoenixg 发表于 2011-7-24 13:32:20

jeongee 发表于 2011-7-24 13:29 static/image/common/back.gif
哦,是你传进去的object变量有问题,你传的是类名的字符串。。。。怎么会有session。
让你的$object = &...

谢谢超版!
还有以下不明

1 让你的$object = & get_instance();吧 要在哪里改啊
2 你的文件命名会有问题的,你可能现在用的windows,它不区分大小写,如果你传到linux等下,就会报错了。文件应该怎么命名才能在linux下正常呢?

jeongee 发表于 2011-7-24 13:34:43

phoenixg 发表于 2011-7-24 13:32 static/image/common/back.gif
谢谢超版!
还有以下不明



1.$object = & get_instance();当然是在你的helper函数里面了
2.首字母小写!

phoenixg 发表于 2011-7-24 13:35:39

jeongee 发表于 2011-7-24 13:34 static/image/common/back.gif
1.$object = & get_instance();当然是在你的helper函数里面了
2.首字母小写!

万分感谢!

phoenixg 发表于 2011-7-24 13:44:37

这样是可以了,但我觉得每次都写$CI = & get_instance();是不是多余了,有否可能再改进下?

article.php控制器

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

class Article extends CI_Controller {

    function __construct()
    {
      parent::__construct();
      $this->load->helper('MY_others');
      $CI = & get_instance();
      is_logged_in($CI);
    }


my_others_helper.php 自定义辅助函数(位于application/helpers下,已改成小写文件名)

if ( ! function_exists('is_logged_in'))
{
    function is_logged_in($CI)
    {
      $is_logged_in = $CI->session->userdata('is_logged_in');
      if(!isset($is_logged_in) || $is_logged_in != true)
      {
            echo 'You don\'t have permission to access this page.&nbsp;';
            echo anchor('member/index','Login');   
            die();   
      }   
    }
}



visvoy 发表于 2011-7-26 09:46:30

我勒个去,j是让你把代码写在helper里面,不是controller

phoenixg 发表于 2011-7-26 14:23:30

visvoy 发表于 2011-7-26 09:46 static/image/common/back.gif
我勒个去,j是让你把代码写在helper里面,不是controller

弱弱问一句,那要怎么移动?
页: [1] 2
查看完整版本: 着急,有关自定义辅助函数的问题