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

一个简单问题,控制器里面,如何申明一个变量,让每个...

[复制链接]
发表于 2012-10-22 11:15:22 | 显示全部楼层 |阅读模式
PHP复制代码
 
<?php
class Blog extends CI_Controller {
 
 function __construct()
 {
  parent::__construct();
 }
$str = '申明'
 public function index()
 {
  echo $str.'1';
 }
 
 public function sec()
 {
  echo $str.'2';
 }
}
?>
 
复制代码


发表于 2012-10-22 11:18:43 | 显示全部楼层
最简单的PHP你都没有看, var $str='';调用时$this->$str

http://www.php.net/manual/zh/keyword.class.php

类是变量与作用于这些变量的函数的集合。变量通过var来定义,函数通过 function 来定义,
 楼主| 发表于 2012-10-23 11:35:51 | 显示全部楼层
PHP复制代码
 
<?php
class His_data extends MY_Controller {
 
        var $table = '2012ppt;
        function __construct()
        {
                parent::__construct();
 
        }
 
 
        function index()
        {
               
                echo $this->$table;
        }
 
}
?>

 
复制代码



结果:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: table
Filename: controllers/his_data.php
Line Number: 15

发表于 2012-11-4 09:31:32 | 显示全部楼层
要写  $this->table;

本版积分规则