-1

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

tereško
  • 58,060
  • 25
  • 98
  • 150
Neil
  • 21
  • 4

3 Answers3

1

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);         
}
Karsten Koop
  • 2,475
  • 1
  • 18
  • 23
0

Alright - look closely to your function:

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;
}

So, you function returns either true of false. And this boolean values is assgined to $this->datacol here:

$this->datacol = $this->load($catid);     

As this assignment happens after you did $this->datacol = mysqli_fetch_array($res);, your $this->datacol becomes either true or false, and holds no real data from mysql.

So, one of the solutions can be to remove assignment:

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

In this case your $this->datacol is not overwritten.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
0

its simple $this->datacol = $this->load($catid); is not required

you have assigning $this->datacol twice

 public function __construct($catid=0)
    {
        $this->catid=0;
        $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;
}

please add if anything is missing

Mahesh Hegde
  • 1,131
  • 10
  • 12