Witam,
Czy ktoś siedzący na co dzień w programowaniu obiektowym, mógłby sprawdzić mój kod pod kątem czytelności, poprawnych typów zmiennych, logikę kodu. Coś zmienić, poprawić, usunąć, unikać, stosować na przyszłość?
Pierwszy raz napisałem coś obiektowo i dlatego chce się zwrócić do nas po wasze zdanie.
Z góry dzięki
<?php
class Connect{
private $host = "localhost";
private $dbname = "obiektowosc";
private $root = "root";
private $password = "";
protected $connect;
public function __construct(){}
protected function Connect(){
try{
$this -> connect = new PDO("mysql:host=".$this -> host.";dbname=".$this -> dbname, $this -> root, $this -> password);
$this -> connect -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo "błąd połączenia z bazą: ". $e -> getMessage();
}
}
}
class AddPerson extends Connect{
private $name;
private $surname;
private $age;
public function __construct($name, $surname, $age){
parent::__construct();
parent::Connect();
$this -> name = trim($name);
$this -> surname = trim($surname);
$this -> age = trim($age);
}
function AddInformation(){
try{
$addInformation = "
INSERT INTO informacje (id, imie, nazwisko, wiek)
VALUES (null, :name, :surname, :age)";
$bind = $this -> connect -> prepare($addInformation);
$bind -> bindValue(':name', $this -> name, PDO::PARAM_STR);
$bind -> bindValue(':surname', $this -> surname, PDO::PARAM_STR);
$bind -> bindValue(':age', $this -> age, PDO::PARAM_INT);
$bind -> execute();
$bind = null;
header("Location: ".$_SERVER["REQUEST_URI"]);
}
catch(PDOException $e){
echo "błąd dodania do bazy: ".$e -> getMessage();
}
}
}
class ShowPersons extends Connect{
private $person = [];
public function __construct(){
parent::__construct();
parent::Connect();
}
function ShowInformation(){
try{
$viewPersons = "SELECT * FROM informacje";
foreach($this -> connect -> query($viewPersons) as $index => $value){
$this -> person[$index] = $value;
}
$this -> connect = null;
return $this -> person;
}
catch(PDOException $e){
echo "nie można wyświetlić informacji: ". $e -> getMessage();
}
}
}
if(!empty($_POST["imie"]) && !empty($_POST["nazwisko"]) && !empty($_POST["wiek"])){
$add_person = new AddPerson($_POST["imie"], $_POST["nazwisko"], $_POST["wiek"]);
$add_person -> AddInformation();
}
?>
<!doctype html>
<html>
<head>
<title>Obiektowość</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form method="post">
<label>Imię: <input type="text" name="imie" /></label>
<label>Nazwisko: <input type="text" name="nazwisko" /></label>
<label>Wiek: <input type="text" name="wiek" /></label>
<input type="submit" value="dodaj" />
</form>
<table>
<?php
$view_persons = new ShowPersons();
$view_persons = $view_persons -> ShowInformation();
foreach($view_persons as $row){
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["imie"]; ?></td>
<td><?php echo $row["nazwisko"]; ?></td>
<td><?php echo $row["wiek"]; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
Jakby co to moge przesłać jakoś plik txt dla ułatwienia.