Précédent   Forum des professionnels en informatique > PHP > Langage > Syntaxe
Syntaxe Forum d'entraide sur la syntaxe de PHP et la POO. Avant de poster -> FAQ syntaxe, Cours d'initiation et cours de POO
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 04/09/2007, 11h31   #1
Invité de passage
 
Inscription : juin 2007
Messages : 4
Détails du profil
Informations forums :
Inscription : juin 2007
Messages : 4
Points : 2
Points : 2
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 :
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";
	}
rugueux est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 04/09/2007, 11h35   #2
Membre Expert
 
Inscription : janvier 2005
Messages : 2 288
Détails du profil
Informations forums :
Inscription : janvier 2005
Messages : 2 288
Points : 2 287
Points : 2 287
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.
koopajah est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 04/09/2007, 11h47   #3
Invité de passage
 
Inscription : juin 2007
Messages : 4
Détails du profil
Informations forums :
Inscription : juin 2007
Messages : 4
Points : 2
Points : 2
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 !
rugueux est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 26/09/2011, 10h44   #4
Invité de passage
 
Femme
Développeur .NET
Inscription : 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 : 2
Points : 2
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 :
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?
eemmaa est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 26/09/2011, 14h49   #5
Membre actif
 
Homme Florian
Chef de projet NTIC
Inscription : novembre 2004
Messages : 205
Détails du profil
Informations personnelles :
Nom : Homme Florian
Âge : 26
Localisation : France, Hauts de Seine (Île de France)

Informations professionnelles :
Activité : Chef de projet NTIC
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : novembre 2004
Messages : 205
Points : 159
Points : 159
Cette fois-ci, c'est parce que tu appelles $this sans être dans une classe ! Par exemple ligne 25:
Code :
$sDescriptionFieldName =  $this->_VWork->getFieldObs($workInfos['work_code']);
Donc oui pour accéder à this, il faut bien être dans une classe instanciée...
NaeiKinDus est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 09h24.


 
 
 
 
Partenaires

Hébergement Web