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 :

[POO] Using $this when not in object context


Sujet :

Langage PHP

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Juin 2007
    Messages : 5
    Points : 7
    Points
    7
    Par défaut [POO] Using $this when not in object context
    Voilà le message que je reçois, alors que je me trouve bien dans un objet, je ne comprend pas ce que j'ai fais de travers, est-ce que quelqu'un y voit plus clair ?

    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
     
    class farms { 
    	public $html;
     
    	function display_farmsTable($rows){
    			//echo $this->html;
    			$this->html["farmlist"] .= "<table>\n";
    			//header
    			$this->html["farmlist"] .= "\t<tr>\n";
    			$this->html["farmlist"] .= "\t\t<td>Nom</td>\n";
    			$this->html["farmlist"] .= "\t\t<td>CP</td>\n";
    			$this->html["farmlist"] .= "\t\t<td>Ville</td>\n";
    			$this->html["farmlist"] .= "\t</tr>\n";	
     
    		foreach($rows as $row){
    			$this->html["farmlist"] .= "\t<tr>\n";
    			//$this->html["farmlist"] .= "\t\t<td>$field</td>\n";
    			$this->html["farmlist"] .= "\t\t<td><a href='index.php?option=com_farms&id={$row["id"]}&task=displayfarm'>{$row ["name"]}</a></td>\n";
    			$this->html["farmlist"] .= "\t\t<td>{$row ["zipcode"]}</td>\n";
    			$this->html["farmlist"] .= "\t\t<td>{$row ["city"]}</td>\n";
    			$this->html["farmlist"] .= "\t</tr>\n";
    		}
    		$this->html["farmlist"] .= "</table>\n";
    	}

  2. #2
    Membre expert
    Inscrit en
    Janvier 2005
    Messages
    2 291
    Détails du profil
    Informations forums :
    Inscription : Janvier 2005
    Messages : 2 291
    Points : 3 212
    Points
    3 212
    Par défaut
    Ca dépend quand et comment tu appelles ta fonction. Ta classe ne "semble" pas avoir de constructeur, or $this réfère à l'instance courante de ta classe, il faut donc qu'elle ait d'abord été instanciée.
    Pourrais tu donner plus de coder : constructeur + endroit où la fonction display est utilisée.

    EDIT: j'ajouterai aussi que tu utiliser $html comme un tableau mais que tu ne l'as pas initilalisé en array() dans la classe donc il faut que ca soit au moins fait dans le constructeur avant de pouvoir l'utiliser directement comme ca.

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Juin 2007
    Messages : 5
    Points : 7
    Points
    7
    Par défaut
    Merci, tu avais raison, c'était le contructeur qui manquait, et surtout l'appel qui n'était pas correct... Ca fait quelques temps que j'ai pas fait de php, et je jongle un peu...
    Merci pour ton aide !

  4. #4
    Candidat au Club
    Femme Profil pro
    Développeur .NET
    Inscrit en
    Septembre 2011
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2011
    Messages : 3
    Points : 4
    Points
    4
    Par défaut problème similaire
    Bonjour, j'ai le même message d'erreur! après avoir repris du code déjà existant (et peu compréhensible!)
    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
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    <?php
    // MTAO Div
     
    /*
     * This function returns the content of a column (left or right)
     * Be acreful, we need to pass as argument the TWording and Translate, because this is not visible in the function
     */
    $Translate = Zend_Registry::get('Zend_Translate');
     
    /*
     * DISPLAY SETTING
     * 
     */
     
    $itemsWorkType .= "<div class='work'>";
     
    // Comments 	
    $itemsWorkType .="<div class='comments'>
    					<h2>".$this->_helper->DecodeTranslation('Notes', 1)."</h2>
    					<div class='hr'></div>
    					";
    $itemsWorkType .= "<table class='tableFields'>";
     
    // Get the name of the description field:
    $sDescriptionFieldName =  $this->_VWork->getFieldObs($workInfos['work_code']);
     
    if ($workInfos[$sDescriptionFieldName] == '1'){
     
    	$commentValue = $this->_helper->DecodeTranslation('No notes have been added.', 1);
    } else {
    	$commentValue = $workInfos[$sDescriptionFieldName];
    }
     
    $itemsWorkType .= "<tr>
    					<td><div class='commentsValue'>".$commentValue."</div></td>
    					</tr>";
     
    // end Comments
    $itemsWorkType .= "</table></div>
    				   <hr class='separation'/>
    					";
     
    /*
     * We extract the fields with the SQL command DESCRIBE 
     * We keep all the fields excepted those with 'obs' and with work_code
     */
    $workDescFields = array();
    foreach ( $this->_VWork->getFields($workInfos['work_code']) as $fieldName)
    {
    	if(!preg_match('#work_code#',$fieldName) && !preg_match('#obs#',$fieldName))
    		$workDescFields[] = $fieldName;
    }
     
     
    $iMax = count($workDescFields);
     
    if ($iMax > 5 )//In this case we do 2 columns
    {
    	// MTAO Fields Description (col-left)
    	$itemsWorkType .="<div class='col-left'>
    					  <h2>".$this->_helper->DecodeTranslation('Description', 1)."</h2>
    					  <div class='hrsmallblock'></div>";
     
    	$itemsWorkType .= workColumnContent ($this->_TWording, $Translate, $workDescFields, $workInfos, 0 , (int)($iMax/2) );
     
    	$itemsWorkType .= "</div>";//eof col_left
     
    	// work Fields Description (col-right)
    	$itemsWorkType .="<div class='col-right'>
    					  <h2>".$this->_helper->DecodeTranslation('Further information ', 1)."</h2>
    					  <div class='hrsmallblock'></div>";
     
    	$itemsWorkType .= workColumnContent ($this->_TWording, $Translate, $workDescFields, $workInfos, (int)($iMax/2), $iMax );
     
    	$itemsWorkType .= "</div>";//eof col_right
     
    } else {//only one column
     
    	// work Fields Description (col-left)
    	$itemsWorkType .="<div class='col-left'>
    					  <h2>".$this->_helper->DecodeTranslation('Description', 1)."</h2>
    					  <div class='hrsmallblock'></div>";
     
     
    	$itemsWorkType .= workColumnContent ($this->_TWording, $Translate, $workDescFields, $workInfos, 0 , (int)($iMax) );
     
    	$itemsWorkType .= "</div>";//eof col_left
     
    }
     
    //end of request					
    $itemsWorkType .= "</div>";
     
     
     
     
    function workColumnContent ($Wording, $Translate, $workDescFields, $workInfos, $startIndex, $stopIndex ) {
     
    	$glassValueList = array('Tempered', 'Laminated');
    	$typeValueList  = array('Fixe', 'Mobile');
    	$gabValueList   = array('With', 'Without');
     
    	$columnContent = "<table class='tableFieldsWithBorders'>";
     
    	for ($i=$startIndex; $i< $stopIndex; $i++) {
    		$fieldLib = $Wording->getLibForField($workDescFields[$i]);
    		$fieldValue = $workInfos[$workDescFields[$i]];
     
    		//For the empty value we indicate a '-'
    		if ($fieldValue == '')
    			$fieldValue = '-';
     
    		switch ($workDescFields[$i]) {
    			case 'mtao_glass_nature':
    				$fieldValue = $glassValueList[$fieldValue];
    				break;
     
    			case 'mtao_glass_type':
    				$fieldValue = $typeValueList[$fieldValue];
    				break;
     
    			case 'mtao_gab_state':
    				$fieldValue = $gabValueList[$fieldValue];
    				break;
    			case 'mtao_begin_prod':	
    				$fieldValue = preg_replace('#(\d{4})-(\d{2})-(\d{2})#', '$3/$2/$1', $fieldValue);
    				break;
    		}
    		$columnContent  .= "<tr><td><div class='label'>".$this->_helper->DecodeTranslation($fieldLib)."</div></td>
    							   <td><div class='value'>".$Translate->translate($fieldValue)."</div></td></tr>";
    	}
     
    	$columnContent  .= "</table>";
     
    	return $columnContent ;
     
    }
    dois je forcément créer un classe? un constructeur?

  5. #5
    Invité
    Invité(e)
    Par défaut
    Cette fois-ci, c'est parce que tu appelles $this sans être dans une classe ! Par exemple ligne 25:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $sDescriptionFieldName =  $this->_VWork->getFieldObs($workInfos['work_code']);
    Donc oui pour accéder à this, il faut bien être dans une classe instanciée...

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

Discussions similaires

  1. Réponses: 2
    Dernier message: 21/05/2014, 09h33
  2. Using $this when not in object context
    Par kate59 dans le forum Langage
    Réponses: 6
    Dernier message: 02/09/2010, 13h54
  3. [PHP 5.0] [POO] Using $this when not in object context
    Par Gaetch dans le forum Langage
    Réponses: 10
    Dernier message: 06/03/2010, 11h35
  4. Réponses: 7
    Dernier message: 16/07/2009, 08h13
  5. Fatal error: Using $this when not in object context
    Par lavande dans le forum Langage
    Réponses: 2
    Dernier message: 02/10/2008, 20h17

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