【幫分析】這樣用OOP是錯誤的嗎?PHP的一個OOP報錯~

來源:互聯網
上載者:User
作用:根據指定ID返回資料(網址)
代碼:

Parse error:  parse error, unexpected T_VARIABLE in PHPDocument1 on line 6
//表 goods public function Goods($goods_id) { $sql = "SELECT g.goods_id,c.file_dir,c.parent_id,c.is_show AS c_is_show,g.is_show AS g_is_show ". " FROM ".self::$ecs->table('goods')." g ". " LEFT JOIN ".self::$ecs->table('category')." c ON g.cat_id=c.cat_id ". " WHERE g.goods_id='$goods_id' "; $rs = self::$db->getRow($sql); $arr = array(); if(!empty($rs)) { $arr['savepath'] = ($rs['g_is_show'] == 1 && $rs['c_is_show'] == 1) ? '/product/'.($rs['file_dir'] ? $rs['file_dir']:$rs['cat_id']).'/'.$rs['goods_id'].'.html' : ''; $arr['remoteurl'] = '/goods.php?id='.$rs['goods_id']; } return $arr; } //表 category public function Category($cat_id,$page = 1) { $sql = "SELECT c.cat_id,c.file_dir,c.parent_id,c.is_show ". " FROM ".self::$ecs->table('category')." c ". " WHERE c.cat_id='$cat_id' "; $rs = self::$db->getRow($sql); $arr = array(); if(!empty($rs)) { $arr['savepath'] = $rs['is_show'] == 1 ? '/product/'.($rs['file_dir'] ? $rs['file_dir']:$rs['cat_id']).'/index'.($page > 0 ? '-'.$page:'').'.html':''; $arr['remoteurl'] = '/category.php?id='.$rs['cat_id'].'&page='.$page; } return $arr; } //表article public function Article($article_id) { $sql = "SELECT a.article_id,c.cat_id,c.file_dir,c.is_open AS c_is_open ". " FROM ".self::$ecs->table('article')." a ". " LEFT JOIN ".self::$ecs->table('article_cat')." c ON a.cat_id=c.cat_id ". " WHERE a.article_id='$article_id' "; $rs = self::$db->getRow($sql); $arr = array(); if(!empty($rs)) { $arr['savepath'] = $rs['is_open'] == 1 ? '/article/'.($rs['file_dir'] ? $rs['file_dir']:$rs['cat_id']).'/'.$rs['article_id'].'.html' : ''; $arr['remoteurl'] = '/article.php?id='.$rs['article_id']; } return $arr; } //表 public function ArticleCat($cat_id,$page = 1) { $sql = "SELECT c.cat_id,c.file_dir,c.parent_id ". " FROM ".self::$ecs->table('article_cat')." c ". " WHERE c.cat_id='$cat_id' "; $rs = self::$db->getRow($sql); $arr = array(); if(!empty($rs)) { $arr['savepath'] = '/article/'.($rs['file_dir'] ? $rs['file_dir']:$rs['cat_id']).'/index'.($page > 0 ? '-'.$page:'').'.html'; $arr['remoteurl'] = '/article_cat.php?id='.$rs['cat_id'].'&page='.$page; } return $arr; }}



用法:
$arr = UrlPath::Goods(1024);print_arr($arr);


另外:開頭有一個報錯,不能這樣寫? procted $db = $GLOBALS['db'];


提問:
一:我想使用UrlPath::Goods($goods_id) 這樣的方式獲得指定的網址和靜態地址,
如何在“類” UrlPath 中不初始化這個類就可以使用公用的 $db 和 $ecs ?

二:我的這個寫法是不是有點問題?~ 另外那裡為什麼會報錯了?以前貌似可以這樣寫。?




回複討論(解決方案)

類變數不能使用變數的值來聲明,需要額外寫一個架構函數__construct來賦值

樓上說的對。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.