Heim >Backend-Entwicklung >PHP-Tutorial >【帮分析】这样用OOP是错误的吗?PHP的一个OOP报错~

【帮分析】这样用OOP是错误的吗?PHP的一个OOP报错~

WBOY
WBOYOriginal
2016-06-23 14:06:381065Durchsuche

作用:根据指定ID返回数据(网址)
代码:

<?phpclass UrlPath{    protected $db = $GLOBALS['db']; //报错    protected $ecs = $GLOBALS['ecs']; //报错 <b>Parse error</b>:  parse error, unexpected T_VARIABLE in <b>PHPDocument1</b> on line <b>6</b><br />    //表 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来赋值

楼上说的对。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn