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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀