C
C
ChEl2020-04-26 09:02:13
PHP
ChEl, 2020-04-26 09:02:13

The value of the object property at the time of use is null even though it is not null in the constructor. Why?

There is a class

class ContentHandler{
        private $context;

        public function __constructor(AppContext $context){
            if($context == null) echo 'BEGINNULL2_';
            $this->context = $context;
            if($this->context == null) echo 'BEGINNULL3_';
        }
        public function getContext():AppContext{
            return $this->context;
        }
        public function parse($resource, $isContent = false){

if($this->getContext() == null) echo 'NULL0_';

            return $this->deployModules($content);
        }

    }


I get an error

Fatal error: Uncaught TypeError: Return value of ContentHandler::getContext() must be an instance of AppContext, null returned in /Library/WebServer/Documents/ingine2/classes/ContentHandler.php:14


AppContext regular class
class AppContext {
  private $headers;
    private $params;
    private $session;
    private $dbConn;
    private $requestCookies;
    private $responseCookies;

    public function __construct($headers, $params, $cookies, $dbConn){
        $this->headers = $headers;
        
        $this->params = $params;
        // nice urls
        // qs contains trailing nice url except first /resourcename/
        if(strlen($this->getParam('qs'))) {

            $qsparams = explode('/',$this->getParam('qs'));
            for($i=0;$i<count($qsparams);$i+=2)
                $this->params[$qsparams[$i]] = $qsparams[$i+1];
        }

        $this->requestCookies = $cookies;
        $this->dbConn = $dbConn;

        $this->session = []; // TODO initiate and restore session
    }

    public function getParam($paramName){
    	return (isset($this->params[$paramName])?$this->params[$paramName]:false);
    }

    public function getHeader($headerName){
        return (isset($this->headers[$headerName])?$this->headers[$headerName]:false);
    }

    public function getCookie($cookieName){
        return $this->requestCookies[$cookieName];
    }

    public function setCookie($cookieName, $value){
        return $this->responseCookies[$cookieName] = $value;
    }

    public function getSessionParam($paramName){
        return $this->session[$paramName];
    }

    public function setSessionParam($paramName, $value){
        $this->session[$paramName] = $value;
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question