Bonjour,

Je me lance dans la POO et j'ai quelques questions. Voici mon 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
60
<?php
class Lang
{
	public $lang;
	public $currentLang;
	public $authorizedLang = array("fr", "en");
	public $defaultLang = "en";
	public $uriLang = substr($_SERVER["REQUEST_URI"], 1, 2);
	public $browserLang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
 
	function __construct()
	{
		if(isset($_SESSION['lang']))
		{
			$this->currentLang = $_SESSION['lang'];
		}
		elseif(in_array($this->browserLang,$this->authorizedLang))
		{
			$this->currentLang = $this->browserLang;
		}
		elseif(in_array($this->uriLang,$this->authorizedLang))
		{
			$this->currentLang = $this->uriLang;
		}
 
		$this->detectLang();
	}
 
	function detectLang()
	{
		/*
		 * 1. if the uri lang is not the same as the current one and is authorized
		 * 2. if the lang is stored in the session
		 * 3. if the browser language is authorized
		 * 4. else we take the defaul language
		 * 5. we store the lang into the session
		*/
 
		if($this->uriLang != $this->currentLang && in_array($this->uriLang, $this->authorizedLang))
		{
			$this->lang = $this->uriLang;
		}
		elseif(in_array($this->currentLang,$this->authorizedLang))
		{
			$this->lang = $_SESSION['lang'];
		}
		elseif(in_array($this->browserLang,$this->authorizedLang))
		{
			$this->lang = $this->browserLang;
		}
		else
		{
			$this->lang = $this->defaultLang;
		}
 
		$_SESSION['lang'] = $this->currentLang;
		//require_once("./data/lang/".$this->lang.".php");
	}
}
?>
Tout d'abord j'ai l'erreur suivante :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
Parse error: syntax error, unexpected '(', expecting ',' or ';' in /Users/me/Site/includes/lang.class.php on line 8
L'erreur provient de l'utilisation de $_SERVER mais je ne comprends pas pourquoi car c'est une variable superglobal qui devrait être accessible dans le scope de ma class non ? Quand j'utilise $_SERVER j'obtiens :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
public $uriLang = $_SERVER["REQUEST_URI"];
Parse error: syntax error, unexpected T_VARIABLE in /Users/me/Site/includes/lang.class.php on line 8
La deuxième question est plus générale, étant donné que ce sont mes premières lignes, est-ce que c'est bon ? Est-ce que je pourrais faire mieux, devrais-je utiliser du static par exemple ? Bref tout ce qui pourrait me faire m'améliorer.


Merci d'avance pour votre aide.