11

PHP - Save the value of a variable

 3 years ago
source link: https://www.codesd.com/item/php-save-the-value-of-a-variable.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

PHP - Save the value of a variable

advertisements

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 ?


I am really not sure what you want to achieve with your code, but let me make 2 comments that might help:

  1. the dot operator . in PHP is used to concatenate strings. If you want to access members of an object you need to use ->. I am just assuming here, but Its a common mistake if you come from C++, I stumbled over this too :)
  2. If you want to work with JSON I recommend using the build in functions json_encode() and json_decode(). Just create an array with your needed elements and use json_encode to convert them to JSON notation. see also http://php.net/manual/de/function.json-encode.php

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK