0

I'm trying to get values of array using POO. But I want to do it using exec (I have to).

this is my exec.php

include('PriceList.php');

for($i=0;$i<1100;$i++){
$tableau[]=$i;
}

$lstPrix = new PriceList($tableau);
exec("php execute.php ");

execute.php

include('PriceList.php');
call_user_func( 'PriceList::getLstPrix' );

and a simple class PriceList.class.php

class PriceList
{
    public static $_lstPrix = array();

    public function __construct($lstPrix){  
        self::$_lstPrix = $lstPrix;
    }

    public static function getLstPrix(){        
        return self::$_lstPrix;
    }
}

I'm trying to get the values of my array but it doesn't work. Where am I doing wrong? some help pls.

12
  • POO or OOP you should understand... Commented Oct 9, 2015 at 14:23
  • @CodeGodie: OOP... but in French! Commented Oct 9, 2015 at 14:23
  • 1
    exec calls a script as a seperate process. That process doesn't have access to the internal state of your current PHP script. I don't know why you would insist on using exec becuase it's probably not the right tool. Commented Oct 9, 2015 at 14:23
  • 1
    oh in French.. :) i understand. just thought it was funny because it sounds like poop. Commented Oct 9, 2015 at 14:23
  • 1
    must be an academic exercise Commented Oct 9, 2015 at 14:30

3 Answers 3

2

The problem in your case is that you set your prices in one process. Then, when the end of the code is reached, your static variable is destroyed.

Then, you launch a new process and try to get the prices. The static variable is empty as it wasn't persisted in any way and it's not the same process. That is why you get an empty array.

I know that what you are trying to achieve here would work in some other cases like an ASP.NET website with IIS as long as you use the same Application pool. If you set the static variable in one request, you can get the value in another request later.

You should save your list somewhere and get it back later. I would consider a database or maybe serialize the data and store it in a file.

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

3 Comments

too bad I can't do that. So if I understand I have to get the exec (script) in the same process
No, you can only spawn a new process with exec, you can pass another argument to exec and it will return that variable populated with the result of the exec
...or save the list somewhere.
1

For reasons well explained in a previous answer, you simply cannot do that.

However, you should know that while you cannot share data between processes, you can pass data from the caller processes to the child process, using various tricks (also known as inter process communication, or IPC).

Passing data from caller to child

Here is one simple method, using argv:

exec.php

$array = range(0,1100);
exec('php execute.php '.json_encode($array));

execute.php

$array = json_decode($argv[1]);

Another method, using stdin:

exec.php

$array = range(0,1100);
exec('echo '.json_encode($array).' | php execute.php');

execute.php

$array = json_decode(fgets(STDIN));

Note: there might be some escaping needed in some cases, check escapeshellargs.

Another method on POSIX systems is to use proc_open instead of exec, and create a writeable pipe to the child stdin and write your data there (check the example in the doc, it's well explained).

Passing results back

Using exec, your child process can just echo JSON, that you can decode in the caller.

With proc_open, you need a pipe on stdout that the child will write to. Also pretty well explained in the PHP doc.

5 Comments

I already tried this way like this escapeshellarg(serialize($lstPrix)) but it can't handle big arrays
@sarikaya serialize is way too verbose. Use json_encode for shorter result (the example above works fine).
It works really fine but sometimes my array is more then 50k rows so when it's really big I have a warning
anyway I'm going to do all in one process and not do the exec. I don't think I have others choices but thanks
@sarikaya You have plenty other choices. I edited my answer with another solution for longer strings (using stdin). And in any cases, proc_open will always work. Or a temporary file for that matter
0

Taken into account all the comments above, you should be able to make this work by sending a JSON and returning a JSON. This is what you can do:

exec.php:

for ($i = 0; $i < 1100; $i++) {
    $tableau[] = $i;
}

$json = json_encode($tableau);
$returned_json = exec("php execute.php $json");
$returned_array = json_decode($returned_json);

var_dump($returned_array);

execute.php

include('PriceList.php');
$array = json_decode($argv[1]);
$lstPrix = new PriceList($array);
$result = $lstPrix::getLstPrix();
echo json_encode($result);

Source about $argv

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.