E
E
Eugene2021-03-09 14:13:52
PHP
Eugene, 2021-03-09 14:13:52

Why is the value assigned to a class variable not saved?

Good afternoon. I continue to delve into OOP, and I am faced with the fact that I do not fully understand the essence of getters and setters.
There is a class:

class Forms {
    private $insertID = '';

public function __construct() {
        // подключение своему классу для работы с PDO (db_pdo.php)
        try {
            require_once 'c:\xampp\htdocs\EnginePHP\class\db_pdo.php';
            $this->myPDO = new DB_PDO();
        } catch(Exeption $e) {
            echo "Ошибка создания класса";
        }
        //
    }
// возвращаем ID вставленной записи
    public function getID() {
        return $this->insertID;
    }

    // устанавливаем ID вставленной записи
    public function setID($insert) {
        $this->insertID = $insert;
    }

    // вывод city и установка префикса
    public function outCity() {
        require 'c:\xampp\htdocs\EnginePHP\polls\city10.php';

        // получаем ID города по префиксу
    if (@$_REQUEST['select']) {
        $data = array($_REQUEST['select']);
        $pref = $this->myPDO->selectWhere("SELECT `id` FROM `city` WHERE `prefix` = ?", $data);
        $id_city = implode('', $pref);

        // вставляем первую запись, получаем номер первой записи, для дальнейшего обновления по номеру записи
        $data = array( 'date_start' => date("Y-m-d H:i:s"), 'id_city' => $id_city );
        var_dump($data);
        $result = $this->myPDO->insert("INSERT INTO `result` (date_start, id_city) values (:date_start, :id_city)", $data);
        if ($result) {
            $this->setID($result);
        } else {
            echo "Insert error!";
        }
        var_dump($this->getID());
    }
    }


    // вывод school с префиксом
    public function outSchool() {
        require 'c:\xampp\htdocs\EnginePHP\polls\school20.php';

            // получаем id текущей записи и обновляем выбор пользователя - школа
        if (@$_REQUEST['select1']) {
            $id = $this->getID();
            var_dump($id); 
            $id_school = ($_REQUEST['select1']);
            $data = array( 'id_school' => $id_school, 'id' => $id );
            var_dump($data); 

            $result = $this->myPDO->update("UPDATE `result` SET `id_school` = :id_school WHERE `id` = :id", $data);
            if ($result == 0) {
                echo "Update error!";
            }
        }
    
    }

    // вывод первой части опроса
    public function outOpros1() {
        require 'c:\xampp\htdocs\EnginePHP\polls\opros30.php';
        $id = $this->getID();
        var_dump($id);
        $age = $_REQUEST['ages'];
        $own = $_REQUEST['familys'];
        $clas = $_REQUEST['clasC'];
        $data = array ('age' => $age, 'own' => $own, 'class' => $clas, 'id' => $id );

        $this->myPDO->update("UPDATE `result` SET `age` = :age, `own` = :own, `class` = :class WHERE `id` = :id", $data);

    }

}


When I insert a record, everything is fine with me, the ID of this record is returned. In the next method, I have to update the record for this ID, but already in the getID () method it is empty. I don't understand. For example, to use prefixes, I use exactly the same getters and setters, everything works. Requests are checked, everything works if instead of a getter slip the usual number of IDs. And the main thing is that it doesn't give any errors, it's just that there is a $this->insertID property, and now it's empty...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Northern Lights, 2021-03-09
@php666

It's impossible to look at this shit code, my eyes are bleeding.
Let's get started
1. Read about class autoloading
2. Stop writing require in methods and read about dependency injection .
3. Stop using the @ dog where it is not required. Namely, in the if (@$_REQUEST['select']) condition, there are isset and empty functions for this.
4. Clearly formulate your question without a bunch of this near-working code and write a small class describing problems with getters and setters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question