I'm new to PHP and met with a little problem.
I think I was previously unclear and we did some changes so let me edit this :
In this part, we work on 2 classes :
First one a Singleton
<?php
class Model {
private $_handle = null;
private function __construct()
{
$this->_handle = array ('0'=>'2011,2015','1'=>[],'2'=>["man","woman"],'3'=>'21,55');
}
public function __clone()
{
}
public static function getInstance()
{
static $_instance = null;
if ($_instance === null) {
$_instance = new self();
}
return $_instance;
}
public function getObj()
{
return $this->_handle;
}
public function setObj($value, $index)
{
$this->_handle[$index]= $value;
}
}
And another class which extends from it.
<?php
include "QueryBuilder.php";
class globalModel extends Model
{
public function valueSettings($type, $val)
{
switch($type)
{
case 'ages':
$this->setObj($val, 3);
break;
case 'sexe':
$this->setObj($val, 2);
break;
case 'countries':
$this->setObj($val, 1);
break;
case 'date':
$this->setObj($val, 0);
break;
}
}
}
public function Main()
{
$this->valueSettings($_POST['type'],$_POST['value']);
$value = $this->getInstance()->getObj();
//Build Query Sentence
// $QB = new QueryBuilder();
// $fullQuery = $QB->QueryBuild($_POST['tab'], $value);
//
// echo $fullQuery;
}
}
We did many experimentation and we found that since the _construct is being ran over and over again, the array we set at the beginning is being reset as construct is being called. That's why we try to use a Singleton here and I think it should be good.
But here, the code doesn't work at all ...
Without a singleton, the Array gets updated for the last value we give it. But if we call valueSettings again, it'll act as if we never called it before because it'll take the value from __construct again.
So the questions would be, is it good idea to use a Singleton ? If yes why does this code isn't working ? =( If no, how should we set the array the first time ? Outside of a function ?