6-Classes et classes DAO Flashcards

1
Q

créer classe

A

class Disque {
// Propriétés
private $artiste;
private $titre;
private $nbChansons;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

instancier obj dune classe

A

$unDisque = new Disque ();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

accès attributs ou methode

A

$disque->artiste = “allo”; //public
$disque->setArtiste(“bob”); //private

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

declarer methode

A

public function setNbChansons(int $qte) : void
{
$this->nbChansons = $qte;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

utilisation this

A

$this->nbChansons

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

exception

A

if ($valeur < 0) ) {
throw new Exception(‘Le nombre de chansons doit être supérieur ou égale à 0’, E_USER_ERROR1);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

constructeur

A

public function __construct(string $artiste,string $titre, int
$nbChansons)
{
$this->setArtiste($artiste);
$this->setTitre($titre);
$this->setNbChansons($nbChansons);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

__toString()

A

public function __toString() : string
{
return $this->getNom() . ‘, ‘ . $this->getPrenom();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Créer classe DAO

A

class PersonneDao
{
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

attribut et constructeur de DAO

A

private $db; // Instance de PDO

public function __construct(PDO $db)
{
$this->setDb($db);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

add

A

public function add(Personne $perso) : void
{
$req = $this->db->prepare(‘INSERT INTO personnes (nom, prenom) VALUES (:nom, :prenom)’);

$req->bindValue(':nom', $perso->getNom(), PDO::PARAM_STR);
$req->bindValue(':prenom', $perso->getPrenom(), PDO::PARAM_STR);

$req->execute();

$req->closeCursor();

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

delete

A

public function delete(Personne $perso) : void
{
$req = $this->db->prepare(‘DELETE FROM personnes WHERE id = :id’);
$req->bindValue(‘:id’, $perso->getId(), PDO::PARAM_INT);
$req->execute();

$req->closeCursor();   }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

get

A

public function get(int $id) : Compte
{
$id = (int) $id;

$req = $this->db->prepare('SELECT nom, anneeNaissance, solde FROM compte WHERE id = :id');
$req->bindValue(':id', $id, PDO::PARAM_INT);
$req->execute();

$compte = $req->fetch();
$objCompte = new Compte($compte['nom'], $compte['anneeNaissance'], $compte['solde']);
// Je dois ajouter le id à la main car le constructeur ne le prend pas en paramètre
$objCompte->setId($id);

$req->closeCursor();

return $objCompte;   }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

update

A

public function update(Compte $compte) : void
{
$req = $this->db->prepare(‘UPDATE Compte SET solde = :solde WHERE id = :id’);

$req->bindValue(':solde', $compte->getSolde(), PDO::PARAM_STR);
$req->bindValue(':id', $compte->getId(), PDO::PARAM_INT);
 
$req->execute();

  $req->closeCursor();   }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

set db

A

public function setDb(PDO $db) : void
{
$this->db = $db;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

get list

A

public function getListPersons() : array
{
$personnes = array();

$req = $this->db->prepare('SELECT id, nom, prenom FROM personnes');
$req->execute();
 
while ($donnees = $req->fetch())
{
  $personne = new Personne($donnees['nom'], $donnees['prenom']);
  $personne->setId($donnees['id']);

  $personnes[] = $personne;
  // ou
  //array_push($personnes, $personne);
	
}

  $req->closeCursor();
 
return $personnes;   }
17
Q
A