Answer the question
In order to leave comments, you need to log in
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);
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question