dsfjk44656 2013-11-18 01:25
浏览 11
已采纳

注意:未定义的属性:[关闭]

I am getting Notice: Undefined property: Page::$vars in C:\xampp\htdocs\mysite.local\ww.php_classes\Page.php on line 41. Then it is showing "404 thing goes here". I am getting this notice whenever I am clicking from navigation menu. But when I type the url mysite.local/safety_tips then the page loads with its contents.

page.php code is as below:

<?php
class Page{
    static $instances = array();
    static $instancesByName = array();
    static $instancesBySpecial= array();

    function __construct($v,$byField=0,$fromRow=0,$pvq=0){
        # byField: 0=ID; 1=Name; 3=special
        if (!$byField && is_numeric($v)){ // by ID
            $r=$fromRow?
            $fromRow:
            ($v?
                dbRow("select * from pages where id=$v limit 1"):
                array()
            );
        } else if ($byField == 1) { // by name
            $name=strtolower(str_replace('-','_',$v));
            $fname='page_by_name_'.md5($name);
            $r=dbRow("select * from pages where name like '"
            .addslashes($name)."' limit 1");
        } else if ($byField == 3 && is_numeric($v)) { // by special
            $fname='page_by_special_'.$v;
            $r=dbRow("select * from pages where special&$v limit 1");
        } else 
            return false;

        if(!count($r || !is_array($r)))return false;

        if(!isset($r['id']))$r['id']=0;

        if(!isset($r['type']))$r['type']=0;

        if(!isset($r['special']))$r['special']=0;

        if(!isset($r['name']))$r['name']='NO NAME SUPPLIED';

        foreach ($r as $k=>$v) $this->{$k}=$v;

        $this->urlname=$r['name'];
        $this->dbVals=$r;
        self::$instances[$this->id] =& $this;
        self::$instancesByName[preg_replace(
            '/[^a-z0-9]/','-',strtolower($this->urlname)
            )] =& $this;

        self::$instancesBySpecial[$this->special] =& $this;

        if(!$this->vars)$this->vars='{}';

        $this->vars=json_decode($this->vars);
    }


    function getInstance($id=0,$fromRow=false,$pvq=false){
        if (!is_numeric($id)) return false;

        if (!@array_key_exists($id,self::$instances))

        self::$instances[$id]=new Page($id,0,$fromRow,$pvq);

        return self::$instances[$id];
    }


    function getInstanceByName($name=''){
        $name=strtolower($name);
        $nameIndex=preg_replace('#[^a-z0-9/]#','-',$name);

        if(@array_key_exists($nameIndex,self::$instancesByName))
            return self::$instancesByName[$nameIndex];

        self::$instancesByName[$nameIndex]=new Page($name,1);
        return self::$instancesByName[$nameIndex];
    }

    function getInstanceBySpecial($sp=0){
        if (!is_numeric($sp)) return false;
        if (!@array_key_exists($sp,$instancesBySpecial))
        $instancesBySpecial[$sp]=new Page($sp,3);
        return $instancesBySpecial[$sp];
    }
}

The index/main page code is as follows:

<?php
    // { common variables and functions
    include_once('/ww.includes/common.php');
    $page=isset($_REQUEST['page'])?$_REQUEST['page']:'';
    $id=isset($_REQUEST['id'])?(int)$_REQUEST['id']:0;
    // }
    // { get current page id

    if(!$id){
        if($page){ // load by name
            $r=Page::getInstanceByName($page);
            if($r && isset($r->id))$id=$r->id;
                unset($r);
            }
        if(!$id){ // else load by special
            $special=1;
            if(!$page){
                $r=Page::getInstanceBySpecial($special);
                if($r && isset($r->id))$id=$r->id;
                unset($r);
            }
        }
    }
// }
// { load page data
    if($id){
        $PAGEDATA=(isset($r) && $r)? $r : Page::getInstance($id);
    } else{
        echo '404 thing goes here';
        exit;
    }
// }
    echo $PAGEDATA->body;

Where did I make mistake?

  • 写回答

1条回答 默认 最新

  • dourunlao1642 2013-11-18 01:28
    关注

    This test is wrong:

    if(!$this->vars)$this->vars='{}';
    

    Use:

    if(!isset($this->vars)) $this->vars='{}';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口