0

i have two classes Session and SupplierSession. SupplierSession is supposed to inherit from Session. how do i ensure that the session_start() in the __construct() method of Session does not get called in SupplierSession. here are the class definitions.

session.php

<?php
    class Session {
        private $loggedIn = false;
        public $userID;

        public function __construct() {
            session_start();
            $this->checkLogin();
        }

        public function isLoggedIn() {
            return $this->loggedIn;
        }

        public function login( $user ) {
            if ( $user ) {
                $this->userID = $_SESSION['userID'] = $user->userID;
                $this->loggedIn = true;
            }
        }

        public function logout() {
            unset( $this->userID );
            unset( $_SESSION['userID'] );
            $this->loggedIn = false;
        }

        private function checkLogin() {
            if ( isset( $_SESSION['userID'] ) ) {
                $this->userID = $_SESSION['userID'];
                $this->loggedIn = true;
            } else {
                unset( $this->userID );
                $this->loggedIn = false;
            }
        }
    }

    $session = new Session();
?>

suppliersession.php
<?php

    class SupplierSession extends Session {
        private $loggedIn = false;
        public $supplierID;

        public function __construct() {
            session_start();
            $this->checkLogin();
        }

        public function isLoggedIn() {
            return $this->loggedIn;
        }

        public function login( $supplier ) {
            if ( $supplier ) {
                $this->supplierID = $_SESSION['supplierID'] = $supplier->supplierID;
                $this->loggedIn = true;
            }
        }

        public function logout() {
            unset( $this->supplierID );
            unset( $_SESSION['supplierID'] );
            $this->loggedIn = false;
        }

        private function checkLogin() {
            if ( isset( $_SESSION['supplierID'] ) ) {
                $this->supplierID = $_SESSION['supplierID'];
                $this->loggedIn = true;
            } else {
                unset( $this->supplierID );
                $this->loggedIn = false;
            }
        }
    }

    $supplierSession = new SupplierSession();
?>
2
Contributors
1
Reply
9
Views
3 Years
Discussion Span
Last Post by urtrivedi
0

Session_start function is usually called in begning of page or from any common file which is included in all page in begning.

Take out it from classes

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.