-1

I'm using a custom PHP framework which is largely based on CodeIgniter.

In one of my controller files, I've set a class property called $orderId. After the user has filled in the form and submitted, I'll do a DB insert, get the order no. and override the $orderId class property value with that order no.

Then I'll redirect to the submit page where I want to access that updated $orderId class property value. This final part is not working, the submit class gets a blank value for property $orderId.

Where am I going wrong pls? The basic example below. Maybe I can't do this because of the redirect and should use a session var instead?

Thanks!

[EDIT] Or I could pass the orderId as the 3rd URL param in the redirect. E.G. redirect('orders/submit/'.self::$orderId); in which case I'll turn all the self:: instances into $this-> for class level scope.

class Orders extends Controller {
    private static $orderId;

    public function __construct() {
        // assign db model
    }

    public function index() {
        if($_SERVER['REQUEST_METHOD'] == 'POST') {

            $data = [
                // form data to pass to db model
            ];

            self::$orderId = 12345; // example order no. return from db model
            if(!empty(self::$orderId)) {
                redirect('orders/submit');
            }
        }
    }

    public function submit() {
        $data = [
            'orderId' => self::$orderId
        ];
        $this->view('orders/submit', $data);
    }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Mshah
  • 61
  • 6
  • The issue itself is a fundamental architecture problem. static only works when you're working with the same instance. But since you're redirecting, the framework is getting reinitialised. Losing the value of your static property. Best way to go about doing this is by storing the order id in a session variable and then read that variable. Sessions last for the as long as the browser window is open. –  Feb 18 '20 at 17:26
  • 1
    @ProSamHDx Thanks. Yes, I've gone the session variable route. I tried adding the orderId as part of the 3rd URL param, which worked fine but I didn't like that solution as the user could've amended that param and the output page would change according. Though it wouldn't have added anything new into the DB, it just looked unprofessional to let the user mess with the output page like that. – Mshah Feb 19 '20 at 10:31

1 Answers1

0

The issue itself is a fundamental architecture problem. static only works when you're working with the same instance. But since you're redirecting, the framework is getting reinitialised. Losing the value of your static property. Best way to go about doing this is by storing the order id in a session variable and then read that variable. Sessions last for the as long as the browser window is open