SlideShare une entreprise Scribd logo
PHP 5 pour les développeurs Java
Mehdi EL KRARI
●

Doctorant à l'université Mohammed V – Agdal
–

●

#Métaheuristiques #TSP #ILS #VNS

Freelancer WEB

elkrari

2 mars 2014

PHP 5 pour les développeurs Java

2
Classe & Attributs...
<?php
class Etudiant
{
  private $_matricule ;
  private $_nom ;
  private $_prenom ;
  private $_moyenne ;
}
?>

class Etudiant
{
  private int matricule ;
  private String nom ;
  private String prenom ;
  private float moyenne ;
}

La notation PEAR (dans PHP) :
[¤]Chaque nom d'élément privé (attributs ou méthodes) doit être précédé
d'un underscore (' _ ').
[¤]Le nom des classes commences par une majuscule.
2 mars 2014

PHP 5 pour les développeurs Java

3
Méthodes
<?php
class Etudiant
{
// déclaration des attributs
        
  public function mention() 
  {
  }     
  public function estAdmis() 
  {
  }
}
?>
2 mars 2014

class Etudiant
{
// déclaration des attributs
        
  public String mention() 
  {
  }     
  public boolean estAdmis() 
  {
  }       
}

PHP 5 pour les développeurs Java

4
Accesseurs (Getters)
<?php
class Etudiant
{
// déclaration des attributs    
  public function matricule()
  {
    return $this->_matricule;
  }     
  public function nom()
  {
    return $this->_nom;
  }    
  public function moyenne()
  {
    return $this->_moyenne;
  }
}
?>
2 mars 2014

class Etudiant
{
// déclaration des attributs    
  public int getMatricule()
  {
    return this.matricule;
  }
  public String getNom()
  {
    return this.nom;
  }    
  public float getMoyenne()
  {
    return this.moyenne;
  }
}
PHP 5 pour les développeurs Java

5
Mutateurs (Setters)
<?php
class Etudiant
{
//déclaration des attributs
public function setMatricule($matricule)
{
$this->_matricule = $matricule;
}
public function setNom($nom)
{
$this->_nom = $nom;
}
public function setMoyenne($moyenne)
{
$this->_moyenne = $moyenne;
}
}
?>
2 mars 2014

class Etudiant
{
//déclaration des attributs
public void setMatricule(int matricule)
{
this.matricule = matricule;
}
public void setNom(String nom)
{
$this.nom = nom;
}
public void setMoyenne(float moyenne)
{
$this.moyenne = moyenne;
}
}

PHP 5 pour les développeurs Java

6
Constructeurs
<?php
class Etudiant
{
  private $_matricule;
  private $_nom;
  private $_prenom;
  private $_moyenne;

class Etudiant
{
  private int matricule;
  private String nom;
  private String prenom;
  private float moyenne;

  public function __construct($nom, $prenom) 
  {
    $this->_$nom= $nom; 
    $this->_$prenom= $prenom; 
    $this->_moyenne= 10; // Initialisation de la
moyenne à 10.
  }
  
}
?>
2 mars 2014

  public Etudiant(String nom,String prenom) 
  {
    this.nom= nom; 
    this.prenom= prenom; 
    this.moyenne= 10; // Initialisation de la
moyenne à 10.
  }
  
}

PHP 5 pour les développeurs Java

7
Instanciation
<?php
$etudiant = new Etudiant() ;

Etudiant etudiant = new Etudiant() ;

$etudiant0 = new Etudiant ;

Etudiant etudiant1 = new
Etudiant('NOM', 'Prenom');

$etudiant1 = new Etudiant('NOM', 'Prenom');
?>

Si la classe n'implémente pas de constructeur ou si le constructeur ne requiert
aucun argument, les parenthèses placées après le nom de la classe lorsqu'on
l'instanciera seront optionnelles. Ainsi, on pourra faire $etudiant = new Etudiant;

2 mars 2014

PHP 5 pour les développeurs Java

8
Auto-chargement de classes (Auto-load)
<?php
function chargerClasse($classe)
{
require $classe . '.class.php'; // On
inclut la classe correspondante au
paramètre passé.
}
spl_autoload_register('chargerClasse'); // On
enregistre la fonction en autoload pour
qu'elle soit appelée dès qu'on instanciera
une classe non déclarée.

L'auto-chargement de classes
n'existe
pas
sur
le
Kit
de
développement Java . Néanmoins, les
environnements de développement
integré (Eclipse,Netbeans,...) signalent
l'absence de classes ou packages et
proposent de rajouter les « import »
manquants si leurs accessibilités le
permet.

$perso = new Etudiant();
?>

2 mars 2014

PHP 5 pour les développeurs Java

9
Constantes de classe
<?php
class Etudiant
{
  private $_matricule;
  private $_nom;
  private $_prenom;
  private $_moyenne;
// Déclarations des constantes en rapport avec
la moyenne.
const PASSABLE = 10;
const ASSEZ_BIEN = 12;
const BIEN = 14;
}
?>

class Etudiant
{
  private int matricule;
  private String nom;
  private String prenom;
  private float moyenne;
// Déclarations des constantes en rapport avec
la moyenne.
public static final float PASSABLE = 10;
public static final float ASSEZ_BIEN = 12;
public static final float BIEN = 14;
}

<?php
$etudiant = new Etudiant;
$etudiant->setMoyenne(Etudiant::ASSEZ_BIEN);
?>

Etudiant etudiant = new Etudiant();
etudiant.setMoyenne(Etudiant.ASSEZ_BIEN);

2 mars 2014

PHP 5 pour les développeurs Java

10
Méthodes statiques
<?php
class Etudiant
{
// déclaration des attributs    

class Etudiant
{
// déclaration des attributs    

public static function message()
{
echo 'Bonjour étudiant(e)';
}

public static void message()
{
System.out.println('Bonjour étudiant(e)');
}

}
?>

}

<?php
Etudiant::message();
$etudiant = new Etudiant(Etudiant::BIEN);
$etudiant->message();
?>
2 mars 2014

Etudiant.message();
Etudiant etudiant = new Etudiant(Etudiant.BIEN);
etudiant.message();

PHP 5 pour les développeurs Java

11
Attributs statiques
<?php
class Etudiant
{
  private $_matricule;
  private $_nom;
  private $_prenom;
  private $_moyenne;

class Etudiant
{
  private int matricule;
  private String nom;
  private String prenom;
  private float moyenne;

private static $_msg = 'Bonjour étudiant(e)';

private static String msg = 'Bonjour
étudiant(e)';

public static function message()
{
echo self::$_msg;
}

public static function message()
{
System.out.print(msg);
//System.out.print(Etudiant.msg);
}

}
?>
}
2 mars 2014

PHP 5 pour les développeurs Java

12
Managers
<?php
class EtudiantManager
{
private $_db; // Instance de PDO.

1/2

}
public function getList()
{
// Retourne la liste de tous les personnages.

}
public function update(Etudiant $etud)
{

// Préparation de la requête d'insertion.
// Assignation des valeurs pour le nom, la force, les dégâts,
l'expérience et le niveau du personnage.
// Exécution de la requête.

}
public function delete(Etudiant $etud)
{
}
2 mars 2014

2/2

// Exécute une requête de type SELECT avec une clause
WHERE, et retourne un objet Personnage.

public function __construct($db)
{
$this->setDb($db);
}
public function add(Etudiant $etud)
{

// Exécute une requête de type DELETE.

public function get($id)
{

// Prépare une requête de type UPDATE.
// Assignation des valeurs à la requête.
// Exécution de la requête.

}
public function setDb(PDO $db)
{
$this->_db = $db;
}
}
?>

PHP 5 pour les développeurs Java

13
Managers(add)
public function add(Etudiant $etud)
{
$q = $this->_db->prepare('INSERT INTO personnages SET nom = :nom, prenom = :prenom, matricule
= :matricule, moyenne = :moyenne');
$q->bindValue(':nom', $etud->nom());
$q->bindValue(':prenom', $etud->prenom(), PDO::PARAM_INT);
$q->bindValue(':matricule', $etud->matricule(), PDO::PARAM_INT);
$q->bindValue(':moyenne', $etud->moyenne(), PDO::PARAM_INT);
$q->execute();
}

2 mars 2014

PHP 5 pour les développeurs Java

14
Managers(delete, get)
public function delete(Etudiant $etud)
{
$this->_db->exec('DELETE FROM personnages WHERE id = '.$etud->id());
}
public function get($id)
{
$id = (int) $id;
$q = $this->_db->query('SELECT id, nom, prenom, matricule, moyenne FROM personnages WHERE id
= '.$id);
$donnees = $q->fetch(PDO::FETCH_ASSOC);
return new Personnage($donnees);
}

2 mars 2014

PHP 5 pour les développeurs Java

15
Managers(getList)
public function getList()
{
$etuds = array();
$q = $this->_db->query('SELECT id, nom, prenom, matricule, moyenne FROM personnages ORDER BY
nom');
while ($donnees = $q->fetch(PDO::FETCH_ASSOC))
{
$etuds[] = new Personnage($donnees);
}
return $etuds;
}

2 mars 2014

PHP 5 pour les développeurs Java

16
Managers(update)
public function update(Etudiant $etud)
{
$q = $this->_db->prepare('UPDATE personnages SET prenom = :prenom, matricule = :matricule,
moyenne = :moyenne WHERE id = :id');
$q->bindValue(':prenom', $etud->prenom(), PDO::PARAM_INT);
$q->bindValue(':matricule', $etud->matricule(), PDO::PARAM_INT);
$q->bindValue(':moyenne', $etud->moyenne(), PDO::PARAM_INT);
$q->bindValue(':id', $etud->id(), PDO::PARAM_INT);
$q->execute();
}

2 mars 2014

PHP 5 pour les développeurs Java

17

Contenu connexe

PPTX
Programmation orientée objet avancée
DOCX
test doc
PPT
Java uik-chap6-poo heritage v2 java
PPTX
c# programmation orientée objet (Classe & Objet)
PPT
Java uik-chap2-dev java
PDF
Chapitre 4 heritage et polymorphisme
PDF
Chapitre 5 classes abstraites et interfaces
Programmation orientée objet avancée
test doc
Java uik-chap6-poo heritage v2 java
c# programmation orientée objet (Classe & Objet)
Java uik-chap2-dev java
Chapitre 4 heritage et polymorphisme
Chapitre 5 classes abstraites et interfaces

Tendances (20)

PPTX
PDF
Héritage et polymorphisme- Jihen HEDHLI
PDF
Introduction java
PPSX
Fondamentaux java
PPT
Java uik-chap1-intro java
PDF
Cours c++
PDF
PDF
Cours java
PPTX
Language java
PPT
Formation C# - Cours 3 - Programmation objet
PDF
Cours de C++, en français, 2002 - Cours 2.2
PPT
JAVA-UIK-CHAP6-POO HERITAGE JAVA
PDF
Hibernate : comment déclarer une entité avec les annotations ?
PPT
programmation orienté objet c++
PDF
[Hibernate] Contexte de persistance et flushing
PDF
Chapitre 3 elements de base de java
PDF
Chapitre6: Surcharge des opérateurs
PPTX
Cpp2 : classes et objets
PDF
Chapitre 2 classe et objet
PPTX
Marzouk architecture encouches-jee-mvc
Héritage et polymorphisme- Jihen HEDHLI
Introduction java
Fondamentaux java
Java uik-chap1-intro java
Cours c++
Cours java
Language java
Formation C# - Cours 3 - Programmation objet
Cours de C++, en français, 2002 - Cours 2.2
JAVA-UIK-CHAP6-POO HERITAGE JAVA
Hibernate : comment déclarer une entité avec les annotations ?
programmation orienté objet c++
[Hibernate] Contexte de persistance et flushing
Chapitre 3 elements de base de java
Chapitre6: Surcharge des opérateurs
Cpp2 : classes et objets
Chapitre 2 classe et objet
Marzouk architecture encouches-jee-mvc
Publicité

En vedette (15)

PPT
Introduction au langage PHP (1ere partie) élaborée par Marouan OMEZZINE
PPTX
Présentation symfony epita
PDF
PHP 5 et la programmation objet
PDF
Php
PDF
Introduction à Laravel 4 @Dogstudio
PPT
PHP 5.3, PHP Next
PPT
Symfony 2 : chapitre 2 - Les vues en Twig
PPT
Symfony 2 : chapitre 3 - Les modèles en Doctrine 2
PPT
Symfony 2 : chapitre 4 - Les services et les formulaires
PPTX
Symfony 2 : chapitre 1 - Présentation Générale
PDF
Application web php5 html5 css3 bootstrap
KEY
Exploiter php 5
PPTX
Php 5.3
PDF
alphorm.com - Formation Développez des applications Web avec ASP.NET MVC 4(70...
PDF
Support JEE Servlet Jsp MVC M.Youssfi
Introduction au langage PHP (1ere partie) élaborée par Marouan OMEZZINE
Présentation symfony epita
PHP 5 et la programmation objet
Php
Introduction à Laravel 4 @Dogstudio
PHP 5.3, PHP Next
Symfony 2 : chapitre 2 - Les vues en Twig
Symfony 2 : chapitre 3 - Les modèles en Doctrine 2
Symfony 2 : chapitre 4 - Les services et les formulaires
Symfony 2 : chapitre 1 - Présentation Générale
Application web php5 html5 css3 bootstrap
Exploiter php 5
Php 5.3
alphorm.com - Formation Développez des applications Web avec ASP.NET MVC 4(70...
Support JEE Servlet Jsp MVC M.Youssfi
Publicité

Similaire à PHP 5 pour les développeurs Java (20)

PPTX
PHP_S4.pptx
PDF
Programmation orientée objet en PHP 5
ODP
Migration PHP4-PHP5
PDF
Présentation de DBAL en PHP (Nantes)
PDF
Php Data Object
PPTX
PHPmsdmskdmskdmskdmksmdksdmkdmssdmksdmkmdskmsdk
PPTX
La première partie de la présentation PHP
ODP
Patterns and OOP in PHP
PDF
chapitre 4-PHP5 module web part2 (1).pdf
PPT
PHP5 - POO
PDF
Résumé Complet : Les Fondamentaux du PHP et Intégration avec MySQL.pdf
PDF
Présentation de DBAL en PHP
PDF
Cours php & Mysql - 2éme partie
PDF
INITIATION_PHP_NAB_2009
PPT
PHP5 et Zend Framework
PDF
Chapitre5_Cours_TechProgWeb_LI2 mr Malek .pdf
PDF
Developpement web dynamique_Base de donnees.pdf
PPTX
S2-02-PHP-objet.pptx
PPT
Hibernate
PDF
PROGRAMMES FASCICULE DE PHP IDA2 (1).pdf
PHP_S4.pptx
Programmation orientée objet en PHP 5
Migration PHP4-PHP5
Présentation de DBAL en PHP (Nantes)
Php Data Object
PHPmsdmskdmskdmskdmksmdksdmkdmssdmksdmkmdskmsdk
La première partie de la présentation PHP
Patterns and OOP in PHP
chapitre 4-PHP5 module web part2 (1).pdf
PHP5 - POO
Résumé Complet : Les Fondamentaux du PHP et Intégration avec MySQL.pdf
Présentation de DBAL en PHP
Cours php & Mysql - 2éme partie
INITIATION_PHP_NAB_2009
PHP5 et Zend Framework
Chapitre5_Cours_TechProgWeb_LI2 mr Malek .pdf
Developpement web dynamique_Base de donnees.pdf
S2-02-PHP-objet.pptx
Hibernate
PROGRAMMES FASCICULE DE PHP IDA2 (1).pdf

PHP 5 pour les développeurs Java

  • 1. PHP 5 pour les développeurs Java
  • 2. Mehdi EL KRARI ● Doctorant à l'université Mohammed V – Agdal – ● #Métaheuristiques #TSP #ILS #VNS Freelancer WEB elkrari 2 mars 2014 PHP 5 pour les développeurs Java 2
  • 3. Classe & Attributs... <?php class Etudiant {   private $_matricule ;   private $_nom ;   private $_prenom ;   private $_moyenne ; } ?> class Etudiant {   private int matricule ;   private String nom ;   private String prenom ;   private float moyenne ; } La notation PEAR (dans PHP) : [¤]Chaque nom d'élément privé (attributs ou méthodes) doit être précédé d'un underscore (' _ '). [¤]Le nom des classes commences par une majuscule. 2 mars 2014 PHP 5 pour les développeurs Java 3
  • 4. Méthodes <?php class Etudiant { // déclaration des attributs            public function mention()    {   }        public function estAdmis()    {   } } ?> 2 mars 2014 class Etudiant { // déclaration des attributs            public String mention()    {   }        public boolean estAdmis()    {   }        } PHP 5 pour les développeurs Java 4
  • 5. Accesseurs (Getters) <?php class Etudiant { // déclaration des attributs       public function matricule()   {     return $this->_matricule;   }        public function nom()   {     return $this->_nom;   }       public function moyenne()   {     return $this->_moyenne;   } } ?> 2 mars 2014 class Etudiant { // déclaration des attributs       public int getMatricule()   {     return this.matricule;   }   public String getNom()   {     return this.nom;   }       public float getMoyenne()   {     return this.moyenne;   } } PHP 5 pour les développeurs Java 5
  • 6. Mutateurs (Setters) <?php class Etudiant { //déclaration des attributs public function setMatricule($matricule) { $this->_matricule = $matricule; } public function setNom($nom) { $this->_nom = $nom; } public function setMoyenne($moyenne) { $this->_moyenne = $moyenne; } } ?> 2 mars 2014 class Etudiant { //déclaration des attributs public void setMatricule(int matricule) { this.matricule = matricule; } public void setNom(String nom) { $this.nom = nom; } public void setMoyenne(float moyenne) { $this.moyenne = moyenne; } } PHP 5 pour les développeurs Java 6
  • 7. Constructeurs <?php class Etudiant {   private $_matricule;   private $_nom;   private $_prenom;   private $_moyenne; class Etudiant {   private int matricule;   private String nom;   private String prenom;   private float moyenne;   public function __construct($nom, $prenom)    {     $this->_$nom= $nom;      $this->_$prenom= $prenom;      $this->_moyenne= 10; // Initialisation de la moyenne à 10.   }    } ?> 2 mars 2014   public Etudiant(String nom,String prenom)    {     this.nom= nom;      this.prenom= prenom;      this.moyenne= 10; // Initialisation de la moyenne à 10.   }    } PHP 5 pour les développeurs Java 7
  • 8. Instanciation <?php $etudiant = new Etudiant() ; Etudiant etudiant = new Etudiant() ; $etudiant0 = new Etudiant ; Etudiant etudiant1 = new Etudiant('NOM', 'Prenom'); $etudiant1 = new Etudiant('NOM', 'Prenom'); ?> Si la classe n'implémente pas de constructeur ou si le constructeur ne requiert aucun argument, les parenthèses placées après le nom de la classe lorsqu'on l'instanciera seront optionnelles. Ainsi, on pourra faire $etudiant = new Etudiant; 2 mars 2014 PHP 5 pour les développeurs Java 8
  • 9. Auto-chargement de classes (Auto-load) <?php function chargerClasse($classe) { require $classe . '.class.php'; // On inclut la classe correspondante au paramètre passé. } spl_autoload_register('chargerClasse'); // On enregistre la fonction en autoload pour qu'elle soit appelée dès qu'on instanciera une classe non déclarée. L'auto-chargement de classes n'existe pas sur le Kit de développement Java . Néanmoins, les environnements de développement integré (Eclipse,Netbeans,...) signalent l'absence de classes ou packages et proposent de rajouter les « import » manquants si leurs accessibilités le permet. $perso = new Etudiant(); ?> 2 mars 2014 PHP 5 pour les développeurs Java 9
  • 10. Constantes de classe <?php class Etudiant {   private $_matricule;   private $_nom;   private $_prenom;   private $_moyenne; // Déclarations des constantes en rapport avec la moyenne. const PASSABLE = 10; const ASSEZ_BIEN = 12; const BIEN = 14; } ?> class Etudiant {   private int matricule;   private String nom;   private String prenom;   private float moyenne; // Déclarations des constantes en rapport avec la moyenne. public static final float PASSABLE = 10; public static final float ASSEZ_BIEN = 12; public static final float BIEN = 14; } <?php $etudiant = new Etudiant; $etudiant->setMoyenne(Etudiant::ASSEZ_BIEN); ?> Etudiant etudiant = new Etudiant(); etudiant.setMoyenne(Etudiant.ASSEZ_BIEN); 2 mars 2014 PHP 5 pour les développeurs Java 10
  • 11. Méthodes statiques <?php class Etudiant { // déclaration des attributs     class Etudiant { // déclaration des attributs     public static function message() { echo 'Bonjour étudiant(e)'; } public static void message() { System.out.println('Bonjour étudiant(e)'); } } ?> } <?php Etudiant::message(); $etudiant = new Etudiant(Etudiant::BIEN); $etudiant->message(); ?> 2 mars 2014 Etudiant.message(); Etudiant etudiant = new Etudiant(Etudiant.BIEN); etudiant.message(); PHP 5 pour les développeurs Java 11
  • 12. Attributs statiques <?php class Etudiant {   private $_matricule;   private $_nom;   private $_prenom;   private $_moyenne; class Etudiant {   private int matricule;   private String nom;   private String prenom;   private float moyenne; private static $_msg = 'Bonjour étudiant(e)'; private static String msg = 'Bonjour étudiant(e)'; public static function message() { echo self::$_msg; } public static function message() { System.out.print(msg); //System.out.print(Etudiant.msg); } } ?> } 2 mars 2014 PHP 5 pour les développeurs Java 12
  • 13. Managers <?php class EtudiantManager { private $_db; // Instance de PDO. 1/2 } public function getList() { // Retourne la liste de tous les personnages. } public function update(Etudiant $etud) { // Préparation de la requête d'insertion. // Assignation des valeurs pour le nom, la force, les dégâts, l'expérience et le niveau du personnage. // Exécution de la requête. } public function delete(Etudiant $etud) { } 2 mars 2014 2/2 // Exécute une requête de type SELECT avec une clause WHERE, et retourne un objet Personnage. public function __construct($db) { $this->setDb($db); } public function add(Etudiant $etud) { // Exécute une requête de type DELETE. public function get($id) { // Prépare une requête de type UPDATE. // Assignation des valeurs à la requête. // Exécution de la requête. } public function setDb(PDO $db) { $this->_db = $db; } } ?> PHP 5 pour les développeurs Java 13
  • 14. Managers(add) public function add(Etudiant $etud) { $q = $this->_db->prepare('INSERT INTO personnages SET nom = :nom, prenom = :prenom, matricule = :matricule, moyenne = :moyenne'); $q->bindValue(':nom', $etud->nom()); $q->bindValue(':prenom', $etud->prenom(), PDO::PARAM_INT); $q->bindValue(':matricule', $etud->matricule(), PDO::PARAM_INT); $q->bindValue(':moyenne', $etud->moyenne(), PDO::PARAM_INT); $q->execute(); } 2 mars 2014 PHP 5 pour les développeurs Java 14
  • 15. Managers(delete, get) public function delete(Etudiant $etud) { $this->_db->exec('DELETE FROM personnages WHERE id = '.$etud->id()); } public function get($id) { $id = (int) $id; $q = $this->_db->query('SELECT id, nom, prenom, matricule, moyenne FROM personnages WHERE id = '.$id); $donnees = $q->fetch(PDO::FETCH_ASSOC); return new Personnage($donnees); } 2 mars 2014 PHP 5 pour les développeurs Java 15
  • 16. Managers(getList) public function getList() { $etuds = array(); $q = $this->_db->query('SELECT id, nom, prenom, matricule, moyenne FROM personnages ORDER BY nom'); while ($donnees = $q->fetch(PDO::FETCH_ASSOC)) { $etuds[] = new Personnage($donnees); } return $etuds; } 2 mars 2014 PHP 5 pour les développeurs Java 16
  • 17. Managers(update) public function update(Etudiant $etud) { $q = $this->_db->prepare('UPDATE personnages SET prenom = :prenom, matricule = :matricule, moyenne = :moyenne WHERE id = :id'); $q->bindValue(':prenom', $etud->prenom(), PDO::PARAM_INT); $q->bindValue(':matricule', $etud->matricule(), PDO::PARAM_INT); $q->bindValue(':moyenne', $etud->moyenne(), PDO::PARAM_INT); $q->bindValue(':id', $etud->id(), PDO::PARAM_INT); $q->execute(); } 2 mars 2014 PHP 5 pour les développeurs Java 17