doraemon0769 2018-01-09 11:07
浏览 57
已采纳

没有从PHP类获得所需的输出

This is my very first question actually. I am under a learning process of OOP PHP and trying to design my first class for Category. Here is my code

<?php
class category{

public  $catid;
public $datacol;

    public function __construct($catid=0)
    {
        $this->catid=0;
        $this->datacol=$this->load($catid);         
    }

    public function load($catid)
    {
        global $conn;
        $sql=' select * '
            .' FROM tbl_categories'
            .' WHERE catid='.$catid;
        if (!($res=mysqli_query($conn,$sql)) || !mysqli_num_rows($res)>0) 
            return false;
        $this->datacol = mysqli_fetch_array($res);
        $this->catid = $this->datacol['catid'];
        return true;
    }

    public function reload() {
        return $this->load($this->getId());
    }

    /* ------------------> Getter methods <--------------------- */
    public function getId() { return $this->catid; }
    public function getName() { return $this->datacol['category']; }
    public function getOneliner() { return $this->datacol['categoryoneliner']; }
    public function getBrief() { return $this->datacol['categorybrief']; }
    public function getThumb() { return $this->datacol['timg']; }
    public function getImage() { return $this->datacol['limg']; }
    public function getParentID() { return $this->datacol['parentcat']; }
    public function getPagetitle() { return  $this->datacol['catpagetitle']; }
    public function getKeywords() { return $this->datacol['catkeywords']; }
    public function getMetadesc() { return $this->datacol['categorymetadesc']; }
    public function getPriority() { return $this->datacol['priority']; }
    public function getRemarks() { return ($this->datacol['remarks']); }
    public function getRow() { return $this->datacol; }

}

?>

Now, When i create its object from other file with a code as below:

$cat=new category(10);
echo var_dump($cat);
echo var_dump($cat->getId());
echo var_dump($cat->getName());

The output gives me the correct catid when called getID(). But Shows NULL for getName() or any other function other than getID().

What am i doing wrong?

Thanks in advance

  • 写回答

3条回答 默认 最新

  • douzheng0702 2018-01-09 11:28
    关注

    As @Arnold Gandarillas said, you are overwriting $datacol in the constructor.

    Your constructor calls load(), which sets $datacol:

    public function load($catid)
    {
        ...
        $this->datacol = mysqli_fetch_array($res);
        return true;
    }
    

    The function then returns true. In the constructor, this true is assigned to $datacol, overwriting the previous value:

    public function __construct($catid=0)
    {
        $this->catid=0;
        $this->datacol=$this->load($catid);         
    }
    

    Change the constructor to something like this:

    public function __construct($catid=0)
    {
        if ($catid > 0)
            $this->load($catid);         
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等