IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

methode non reconu à l'interieur d'une classe


Sujet :

Langage PHP

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    97
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 97
    Points : 59
    Points
    59
    Par défaut methode non reconu à l'interieur d'une classe
    bonjour,
    j'ai ce code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    class Client
    {
    	private $_nom;
    	private $_age;
     
    	function getNom(){return $_nom;}
    	function getAge(){return $_age;}
    	function setNom($nom){$this->_nom=$nom;}
    	function setAge($age){$this->_age=$age;}
     
     
    }
    class GererClient extends Client
    {
        function getConnection()
    	{
    		if (isset($_POST['ok']))
    		{
    		$base=mysql_connect('localhost','root','');
    		mysql_select_db('inscription',$base);
    		}
     
    	}
    	function creerClient($cli)
    	{
    		self::getConnection();
    		$sql=mysql_query("INSERT into client (cle,nom,age) VALUES ('',$cli->getNom(),$cli->getAge()");
     
    	}
     
    }
    le problème c'est à la fonction creerClient,les méthodes getNom() et getAge() sont pas reconu;pourtant la classe GererClient herite de Client,donc les méthodes doivent être reconnu;
    merci pour votre aide

  2. #2
    Membre habitué
    Homme Profil pro
    Inscrit en
    Mars 2009
    Messages
    114
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Mars 2009
    Messages : 114
    Points : 185
    Points
    185
    Par défaut
    le problème c'est que $cli n'est pas reconnu comme un objet Client
    donc supprime la variable $cli et utilise $this->getNom() ...

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    97
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 97
    Points : 59
    Points
    59
    Par défaut
    j'ai essayer ça marche toujours pas;j'ai le même message d'erreur;
    ce que je comprend pas c'est que je récupère les attribut via un formulaire:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    	if(isset($_POST['ok']))
    	{
    	$cli=new Client();
    	$cli->setNom($_POST['nom']);	
    	$cli->setAge($_POST['age']);
    	$gererClient=new GererClient();
    	$gererClient->creerClient();
    	}
    'ok' c'est mon boutton,nom et age 2 zone de texte du formulaire;
    et ça marche pas por récuperer les attributs;

  4. #4
    Membre émérite Avatar de Madfrix
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    2 326
    Détails du profil
    Informations personnelles :
    Localisation : France, Gironde (Aquitaine)

    Informations forums :
    Inscription : Juin 2007
    Messages : 2 326
    Points : 2 566
    Points
    2 566
    Par défaut
    Mets nous ton code complet stp car à changer de code entre 2 posts, on ne s'y retrouve pas

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    97
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 97
    Points : 59
    Points
    59
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    class Client
    {
    	private $_nom;
    	private $_age;
     
    	function getNom(){return $_nom;}
    	function getAge(){return $_age;}
    	function setNom($nom){$this->_nom=$nom;}
    	function setAge($age){$this->_age=$age;}
     
     
    }
    class GererClient extends Client
    {
        function getConnection()
    	{
    		if (isset($_POST['ok']))
    		{
    		$base=mysql_connect('localhost','root','');
    		mysql_select_db('inscription',$base);
    		}
     
    	}
    	function creerClient()
    	{
    		self::getConnection();
    		$sql=mysql_query("INSERT into client (cle,nom,age) VALUES ('',$this->getNom(),$this->getAge()");
     
    	}
     
    }
    <body>
    	<form action="inscription.php" method="post">
    		<fieldset>
    			<legend>informations inscription</legend>
    				<label for="nom">Nom:</label>
    				<input type="text" name="nom"></input><br/>
     
    				<label for="age">Age:</label>
    				<input type="text" name="age"></input>
    				<div id="bouton">
    					<input value="envoyer" name="ok" type="submit"></input>
    				</div>
    		</fieldset>	
    	</form>
     
    	<?php 
     
     
    	if(isset($_POST['ok']))
    	{
    	$cli=new Client();
    	$cli->setNom($_POST['nom']);
    	$cli->setAge($_POST['age']);
    	$gererClient=new GererClient();
    	$gererClient->creerClient();
    	}
    	?>
    </body>
    les méthodes set marche pas la fonction creerClient() non plus;

  6. #6
    Membre habitué
    Homme Profil pro
    Inscrit en
    Mars 2009
    Messages
    114
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Mars 2009
    Messages : 114
    Points : 185
    Points
    185
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    class Client
    {
    	private $_nom;
    	private $_age;
     
    	function getNom(){return $_nom;}
    	function getAge(){return $_age;}
    	function setNom($nom){$this->_nom=$nom;}
    	function setAge($age){$this->_age=$age;}
     
     
    }
    class GererClient extends Client
    {
        function getConnection()
    	{
    		if (isset($_POST['ok']))
    		{
    		$base=mysql_connect('localhost','root','');
    		mysql_select_db('inscription',$base);
    		}
     
    	}
    	function creerClient()
    	{
    		self::getConnection();
    		$sql=mysql_query("INSERT into client (cle,nom,age) VALUES ('',$this->getNom(),$this->getAge()");
     
    	}
     
    }
    <body>
    	<form action="inscription.php" method="post">
    		<fieldset>
    			<legend>informations inscription</legend>
    				<label for="nom">Nom:</label>
    				<input type="text" name="nom"></input><br/>
     
    				<label for="age">Age:</label>
    				<input type="text" name="age"></input>
    				<div id="bouton">
    					<input value="envoyer" name="ok" type="submit"></input>
    				</div>
    		</fieldset>	
    	</form>
     
    	<?php 
     
     
    	if(isset($_POST['ok']))
    	{
     
     
    	$gererClient=new GererClient();
            $gererClient->setNom($_POST['nom']);
    	$gererClient->setAge($_POST['age']);
    	$gererClient->creerClient();
    	}
    	?>
    </body>

  7. #7
    Membre émérite Avatar de Madfrix
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    2 326
    Détails du profil
    Informations personnelles :
    Localisation : France, Gironde (Aquitaine)

    Informations forums :
    Inscription : Juin 2007
    Messages : 2 326
    Points : 2 566
    Points
    2 566
    Par défaut
    2 erreurs relevées :

    1:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    function getNom(){return $this->_nom;}
    function getAge(){return $this->_age;}
     
    // et pas :
    function getNom(){return $_nom;}
    function getAge(){return $_age;}
    2:

    Ensuite, le $this->getNom() fait référence à l'objet $gererClient or les attributs ont éété défini pour l'objet $cli. Le mieux a faire et d'utiliser un seul objet instanciant ta classe enfant et te permettant d'avoir accès à la classe mere :

    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    $obj_enfant = new GererClient();
    $obj_enfant->setNom($_POST['nom']);
    $obj_enfant->setAge($_POST['age']);
    $obj_enfant->creerClient();

    EDIT: semi grillé

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    97
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 97
    Points : 59
    Points
    59
    Par défaut
    j'ai fait les changement,ça marche toujours pas

  9. #9
    Membre émérite Avatar de Madfrix
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    2 326
    Détails du profil
    Informations personnelles :
    Localisation : France, Gironde (Aquitaine)

    Informations forums :
    Inscription : Juin 2007
    Messages : 2 326
    Points : 2 566
    Points
    2 566
    Par défaut
    Regarde le post que j'ai posté juste au dessus dans la meme minute

  10. #10
    Membre habitué
    Homme Profil pro
    Inscrit en
    Mars 2009
    Messages
    114
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Mars 2009
    Messages : 114
    Points : 185
    Points
    185
    Par défaut
    il y a des erreurs dans le code de l'insertion et dans les input
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
     
    <?php
    class Client
    {
    	private $_nom;
    	private $_age;
     
    	function getNom(){return $_nom;}
    	function getAge(){return $_age;}
    	function setNom($nom){$this->_nom=$nom;}
    	function setAge($age){$this->_age=$age;}
     
     
    }
    class GererClient extends Client
    {
        function getConnection()
    	{
    		if (isset($_POST['ok']))
    		{
    		$base=mysql_connect('localhost','root','');
    		mysql_select_db('inscription',$base);
    		}
     
    	}
    	function creerClient()
    	{
    		self::getConnection();
    		$sql=mysql_query("INSERT into client (cle,nom,age) VALUES ('',".$this->getNom().",".$this->getAge().")");
     
    	}
     
    }
    ?>
    <body>
    	<form action="inscription.php" method="post">
    		<fieldset>
    			<legend>informations inscription</legend>
    				<label for="nom">Nom:</label>
    				<input type="text" name="nom"/><br/>
     
    				<label for="age">Age:</label>
    				<input type="text" name="age"/>
    				<div id="bouton">
    					<input value="envoyer" name="ok" type="submit"/>
    				</div>
    		</fieldset>
    	</form>
     
    	<?php
     
     
    	if(isset($_POST['ok']))
    	{
     
     
    	$gererClient=new GererClient();
            $gererClient->setNom($_POST['nom']);
    	$gererClient->setAge($_POST['age']);
    	$gererClient->creerClient();
    	}
    	?>
    </body>

  11. #11
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    97
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 97
    Points : 59
    Points
    59
    Par défaut
    voila mon nouveau code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    <body>
    	<form action="inscription.php" method="post">
    		<fieldset>
    			<legend>informations inscription</legend>
    				<label for="nom">Nom:</label>
    				<input type="text" name="nom"></input><br/>
     
    				<label for="age">Age:</label>
    				<input type="text" name="age"></input>
    				<div id="bouton">
    					<input value="envoyer" name="ok" type="submit"></input>
    				</div>
    		</fieldset>	
    	</form>
     
    	<?php 
    	class Client
    {
    	private $_nom;
    	private $_age;
     
    	function getNom(){return $this->_nom;}
    	function getAge(){return $this->_age;}
    	function setNom($nom){$this->_nom=$nom;}
    	function setAge($age){$this->_age=$age;}
     
     
    }
    	class GererClient extends Client
    {
     
        function getConnection()
    	{
    		if (isset($_POST['ok']))
    		{
    		$base=mysql_connect('localhost','root','');
    		mysql_select_db('inscription',$base);
    		}
     
    	}
     
    	function creerClient()
    	{
    		self::getConnection();
    		$sql=mysql_query("INSERT into client (cle,nom,age) VALUES ('',$this->getNom(),$this->getAge()");
     
    	}
     
    }
     
    	if(isset($_POST['ok']))
    	{
    	$gererClient=new GererClient();
    	$gererClient->setNom($_POST['nom']);
    	$gererClient->setAge($_POST['age']);
    	$gererClient->creerClient();
    	}
    	?>
    </body>
    les méthodes set ça marche par contre les méthodes getNom et getAge de la fonction créer client ça marche pas;

  12. #12
    Membre émérite Avatar de Madfrix
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    2 326
    Détails du profil
    Informations personnelles :
    Localisation : France, Gironde (Aquitaine)

    Informations forums :
    Inscription : Juin 2007
    Messages : 2 326
    Points : 2 566
    Points
    2 566
    Par défaut
    essaies ca :

    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    $sql=mysql_query('INSERT into client (cle,nom,age) VALUES (NULL,'.$this->getNom().','.$this->getAge().')');

  13. #13
    Membre habitué
    Homme Profil pro
    Inscrit en
    Mars 2009
    Messages
    114
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Mars 2009
    Messages : 114
    Points : 185
    Points
    185
    Par défaut
    je t'es donné un nouveau code pour la methode creerClient(), alors change le


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    function creerClient()
    	{
    		self::getConnection();
    		$sql=mysql_query("INSERT into client (cle,nom,age) VALUES (' ',".$this->getNom().",".$this->getAge().")");
     
    	}

  14. #14
    Membre émérite Avatar de Madfrix
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    2 326
    Détails du profil
    Informations personnelles :
    Localisation : France, Gironde (Aquitaine)

    Informations forums :
    Inscription : Juin 2007
    Messages : 2 326
    Points : 2 566
    Points
    2 566
    Par défaut
    Ceci dit, si tu veux coder plus proprement (je trouve), il faudrait que tu passe tes valeurs par le constructeur comme ceci :

    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
     
    class Client{
     
    	private $_nom;
    	private $_age;
     
    	function __construct($nom, $age){
    		$this->_nom = $nom;
    		$this->_age = $age;
    	}
     
    	function __get($attribut){
    		return $this->$attribut;
    	}
     
    }
     
    class GererClient extends Client{
     
    }
     
    $objet = new GererClient('Dupont', 45);
    echo $objet->__get('_age');

  15. #15
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    97
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 97
    Points : 59
    Points
    59
    Par défaut
    merci les gars,c'est bon ;
    juste un petit souci au niveau de l'insertion de la base de données;
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    function creerClient()
    	{
    		self::getConnection();
    		$sql=("INSERT INTO client(cle,nom,age) VALUES ('',".$this->getNom().",".$this->getAge().")");
     
    		mysql_query  ($sql) or die ('Erreur SQL !'.$sql.'<br />'.mysql_error());  
     
    	}
    il prends bien les valeurs que je lui donne mais il y a une erreur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Erreur SQL !INSERT INTO client(cle,nom,age) VALUES ('',alina,5)
    Champ 'alina' inconnu dans field list
    je sais pas d'ou ça vient

  16. #16
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    97
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 97
    Points : 59
    Points
    59
    Par défaut
    c'est bon j'ai trouvé;
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    function creerClient()
    	{
    		self::getConnection();
    		$sql=("INSERT INTO client(cle,nom,age) VALUES ('','".$this->getNom()."',".$this->getAge().")");
     
    		mysql_query  ($sql) or die ('Erreur SQL !'.$sql.'<br />'.mysql_error());  
     
    	}
    il manquer des cotes en plus pour le nom;
    merci

  17. #17
    Membre habitué
    Homme Profil pro
    Inscrit en
    Mars 2009
    Messages
    114
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Mars 2009
    Messages : 114
    Points : 185
    Points
    185
    Par défaut
    met un petit résolu et bonne chance

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 10
    Dernier message: 04/06/2012, 11h29
  2. Valeur non sauvegardé: UserControl Propriété d'une classe
    Par rikidude dans le forum Windows Forms
    Réponses: 0
    Dernier message: 08/12/2009, 22h50
  3. Réponses: 6
    Dernier message: 21/07/2009, 13h53
  4. Pointeur non modifié à l'interieur d'une fonction ? o_O
    Par stephane.lallee dans le forum SL & STL
    Réponses: 8
    Dernier message: 19/03/2009, 16h06
  5. Réponses: 2
    Dernier message: 29/03/2007, 12h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo