Home » Php » php – Change class property value in method A and access updated value in method B

php – Change class property value in method A and access updated value in method B

Posted by: admin February 25, 2020 Leave a comment


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?


[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)) {

    public function submit() {
        $data = [
            'orderId' => self::$orderId
        $this->view('orders/submit', $data);
How to&Answers:

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