0

I am trying to use a structure class so I can use with various classes with same layout. The idea is to access different tables on database with their columns. It works fine but when trying to use multiple variables it overrides previous variables. I want to define new object only once and load data into different variables.

Structure.Class:

class Structure
{
    private $tbl;
    private $info = Array();

    public function __construct($tbl)
    {
        global $db;
        $cols = $db->query("SHOW COLUMNS FROM " . $tbl)->fetchAll();
        foreach ($cols as $col):
            $this->info[$col['Field']] = null;
        endforeach;

        $this->tbl = $tbl;
    }

    public function load($id)
    {
        global $db;
        $select = $db->query("SELECT * FROM ".$this->tbl." WHERE id = ?", $id);
        $helper = Array();
        if ($select->numRows() > 0)
        {
            $results = $select->fetchArray();
            foreach ($results as $key => $r):
                $this->info[$key] = $r;
            endforeach;
            
            return $this;
        }
        return false;
    }
    public function __get($name)
    {
        return $this->info[$name] ?? false;
    }
}

User.Class:

class User extends Structure
{
    private static $table = "users";
    
    public function __construct()
    {
        parent::__construct(self::$table);
    }
}

Usage:

$c['User'] = new User();

$u1 = $c['User']->load(1);
echo $u1->__get('id') . "<hr>";

$u2 = $c['User']->load(2);
echo $u2->__get('id') . "<hr>";

echo $u1->__get('id') . "<hr>";

Why does the last load overrides the first one? And how can I fix it? Thanks!

yivi
  • 42,438
  • 18
  • 116
  • 138
  • It looks like you're trying to implement an ORM. Unless this is just a learning exercise, I recommend you use an existing one. See https://stackoverflow.com/questions/108699/good-php-orm-library for recommendations. – Barmar Feb 22 '22 at 21:47
  • I do prefer to use this version for learning. Do you have any idea how to fix this issue? – Daniel Yeffet Feb 22 '22 at 21:50
  • Because every time you use load, you're setting/resetting `$this->info` array property. User is one object, create another User if you need a new one.. – Bossman Feb 22 '22 at 21:52
  • Maybe there is another way to store the information instead of $this->info? – Daniel Yeffet Feb 22 '22 at 21:54
  • The alternative is to reload the data for user 1, in the same object, when you need it again. – KIKO Software Feb 22 '22 at 21:55

1 Answers1

0

The load() method modifies the given object, it doesn't create a new object.

You need to create multiple User objects.

$u1 = new User();
$u1->load(1);

$u2 = new User();
$u2->load(2);

echo $u2->__get('id') . "<hr>";
echo $u1->__get('id') . "<hr>";
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I am aware of that, but it consumes a lot of resources when there is a lot of data. Is there an option to use only one object and multiple variables? – Daniel Yeffet Feb 22 '22 at 21:53
  • 1
    That makes no sense. If you only have one object, how can it hold multiple values? All your variables are just references to the same object. – Barmar Feb 22 '22 at 21:53
  • There's no magic here. If you need the data for multiple users, you have to consume resources for each of them. – Barmar Feb 22 '22 at 21:54