1

I do try to get static variable of php class in different php files. but when set variable in testpy.php then variable in taski.php is null.

This is testpy.php:

<?php
/**
 * Created by PhpStorm.
 * User: PC1
 * Date: 9/16/2018
 * Time: 3:00 PM
 */
include 'cacheData.php';
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING & ~E_ERROR);
//echo $_POST["firstname"];
cacheData::$cacheArrayFirst = json_decode($_POST["firstname"]);
cacheData::converting(cacheData::$cacheArrayFirst);
echo json_encode(cacheData::$cacheArrayFinal);

this is taski.php:

<?php
/**
 * Created by PhpStorm.
 * User: hamed
 * Date: 17/09/2018
 * Time: 12:37
 */
include 'cacheData.php';
sleep(5);
echo json_encode(cacheData::returnValue());

this is cacheData.php:

<?php
/**
 * Created by PhpStorm.
 * User: PC1
 * Date: 9/16/2018
 * Time: 4:35 PM
 */

class cacheData
{
    public static $cacheArrayFirst;
    public static $cacheArrayFinal;
    public static function converting($cacheArrayOne){
        if (empty(cacheData::$cacheArrayFinal)){
            cacheData::$cacheArrayFinal=$cacheArrayOne;
        }
    }

    public static function returnValue(){
        return self::$cacheArrayFinal;
    }
}
3
  • it's not really clear - are you executing these files (testpy and taski) separately in different HTTP requests? If so, then these are separate execution contexts. Each time you run a script, it's like it never ran before. All the variables are reset each time (barring special cases like session variables). Commented Sep 17, 2018 at 8:59
  • i will call taski.php from android but android show that it is null Commented Sep 17, 2018 at 11:14
  • that wasn't quite what I asked really. But it sounds like you're calling testpy from some other place? As I said, each PHP script runs separately in its own context. It's like the other executions never happened. The variable values are not shared between the different times that the scripts run. If you want to share data between contexts you have to do something like storing it in a database. Commented Sep 17, 2018 at 11:17

1 Answer 1

3

You never call testpy.php from taski.php in any way. Therefore, when taski.php is executed, the code from testpy.php is never run, so the variables are not set.

You could, for example, include testpy.php in taski.php:

<?php
include 'cacheData.php';
include 'testpy.php'; // <-Added
sleep(5);
echo json_encode(cacheData::returnValue());

Possibly, you attempt to access the static variables set by the previous HTTP-call to testpy.php, from taski.php. The sleep could indicate this. It won't work — every HTTP request is a new execution of the PHP application, so all the static variables are reset.

If you need to "preserve" some values between the requests, you need to store tha values in a database, on local drive or in another kind of storage. You can also consider using sessions.

See also: PHP Persist variable across all requests

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.