【问题标题】:Codeigniter - Parse error: syntax error, unexpected '$this'Codeigniter - 解析错误:语法错误,意外'$this'
【发布时间】:2015-02-18 10:05:59
【问题描述】:

我开始学习 codeigniter。我在尝试运行项目时收到此错误。

解析错误:语法错误,意外 '$this' (T_VARIABLE),期待 ';'或 C:\xampp\htdocs\Codeigniter\application\models\post.php 中的“{” 在第 5 行

我的 Model-post.php:

class Post extends CI_Model{
    function get_posts($num=20, $start=0)
        $this->db->select()->from('posts')->where('active',1)->order_by('date_added','desc')->limit($start,$num);
        $query=$this->db->get();
        return $query->result_array();
    }
}

我的 Controller-posts.php:

<?php
class Posts extends CI_Controller{
    function index(){
        $this->load->model('post');
        $data['posts'] = $this->post->get_posts();
        print_r($data['posts']);
    }
}

【问题讨论】:

  • 比较这个函数头:function get_posts($num=20, $start=0)和这个:function index(){
  • 我认为错误在这一行:function get_posts($num=20, $start=0)。您声明一个函数而不打开大括号。像这样添加左大括号 function get_posts($num=20, $start=0) { 令人惊讶的是,您没有打开大括号,而是在倒数第二行关闭它。

标签: php codeigniter


【解决方案1】:
function get_posts($num=20, $start=0)

您缺少此函数的大括号。应该是;

function get_posts($num=20, $start=0) {

【讨论】:

  • @AdrienXL 哇,这个答案只有一个错字才能得到 5 upV,没有其他错误或任何其他需要解释的东西。很遗憾,我在回答问题时发现 7 个错误而没有得到 35 upV。
  • 我也只是在这里发布这个,因为似乎没有多少人读到这个:投票支持关闭 -> 离题:This question was caused by a problem that can no longer be reproduced or a 简单的印刷错误 @987654325 @ 不太可能帮助未来的读者 This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting.
  • @Rizier123 你为什么告诉我这个?即使它不是 SO 中最详尽的答案,它仍然是唯一好的答案,而且 Craig 被不公平地否决了,我只是为了补偿而投了赞成票。没什么大不了的......
  • @AdrienXL 是的,你不应该仅仅因为这个问题就对一个不好的问题投反对票,这是正确的! (meta.stackoverflow.com/q/255459/3933332) 但是,这个答案既不好也没有用。即使是单独的错误消息也会是一个更好的答案。为什么? 1. 它只是 OP 代码中的一个简单的错字而已 2. 这可以在 1 秒内用谷歌搜索,并且在 SO 上已经有重复的曲调 3. 它没有解释(这里还要注意:你甚至看到 OP 了解如何正确编写代码,因为他在另一个地方做对了)
  • 完全同意@Rizier123,而且这个答案可能是一个小时前short and sweet comment + flagged off topic under Typo errors.标记的,
猜你喜欢
  • 2012-06-17
  • 2013-10-11
  • 2015-05-11
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2015-02-24
  • 2012-08-15
  • 1970-01-01
相关资源
最近更新 更多