Codice PHP:
<?php
/**
* Class PropFile
*/
class PropFile
{
////////////////////////// singleton //////////////////////
private static $instance = null;
private static $param = array();
public static function getInstance()
{
if ((null === self::$instance) ) {
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}
private function __construct() {
$this->init();
}
private static function init(){
$file = '..\properties\miofile.properties';
$testo = file_get_contents($file);
$lines = split("\n", $testo);
$key = "";
$isWaitingOtherLine = false;
foreach ($lines as $i => $line) {
if (empty($line) || (!$isWaitingOtherLine && strpos($line, "#") === 0))
continue;
if (!$isWaitingOtherLine) {
$key = substr($line, 0, strpos($line, '='));
$value = substr($line, strpos($line, '=')+1, strlen($line));
file_put_contents('C:\Bitnami\wampstack-5.6.32-1\apache2\logs\erroreCustom.log', 'Valore letto: ' . $value . '\n', FILE_APPEND);
} else {
$value .= $line;
}
/* Check if ends with single '\' */
if (strrpos($value, "\\") === strlen($value)-strlen("\\")) {
$value = substr($value,0,strlen($value)-1)."\n";
$isWaitingOtherLine = true;
} else {
$isWaitingOtherLine = false;
}
file_put_contents('C:\Bitnami\wampstack-5.6.32-1\apache2\logs\erroreCustom.log', 'Chiave letta: ' . $key . '\n', FILE_APPEND);
self::$instance->param[$key] = $value;
file_put_contents('C:\Bitnami\wampstack-5.6.32-1\apache2\logs\erroreCustom.log', 'Parametro letto: ' . self::$instance->param[$key] . '\n', FILE_APPEND);
unset($lines[$i]);
}
}
public function getValue($key){
file_put_contents('C:\Bitnami\wampstack-5.6.32-1\apache2\logs\erroreCustom.log', 'Parametro richiesto: ' . $key . '\n', FILE_APPEND);
$parametro = self::$param[$key];
file_put_contents('C:\Bitnami\wampstack-5.6.32-1\apache2\logs\erroreCustom.log', 'Parametro trovato: ' . $param[$key] . '\n', FILE_APPEND);
return self::$instance->param[$key];
}
}
All'interno della funzione getValue() non riesco a leggere il valore di param[key], nonostante il valore di key che ricevo sia corretto. Sembra che la modifica all'array param resti all'interno della funzione e non venga vista all'esterno. Come posso fare?