Reputation: 476
I have a class that I use to display pages, and I set many class wide variables in the constructor. One of them is coming up null, even though I can see in the call to create the object that the argument is passed. Here is the constructor for the class:
public function __contruct(string $pageTitle, string $page, bool $csrfFlag, string $pageType) {
$this->pageTitle = $pageTitle;
$this->page = $page;
$this->csrfFlag = $csrfFlag;
// Validating page type passed
switch ($pageType) {
case "main":
case "profile":
case "admin":
$this->pageType = $pageType;
default:
throw new Exception("A page type variable was passed that is unknown. That variable was $pageType");
}
}
And here is the particular object creation call:
$display = new PageDisplay('Login', 'login.php', true, 'auth_page');
The problem I'm having is that the variable marked $page isn't being passed through (and I know that because I try and call a late function in the class that displays pages and when it comes to the line about displaying the actual page (in this case, login.php) it gives me this error:Failed opening '' for inclusion
).
Here is that function if you would like to see it:
// Ultimately builds the page to show the user
public function buildPage(bool $needsHeadTags) : void {
// Generate CSRF token if needed
if ($this->csrfFlag === true) { $csrfToken = hash("sha512", random_bytes(64)); }
// Get extra HTML
if ($needsHeadTags === true) { $extraHeadTags = $this->getHeadTags(); }
$headerHtml = $this->getHeader();
$pageTitle = $this->pageTitle;
// Show page
include_once $this->page; // where the error is thrown
}
But I can see that it is passed in the constructor. What am I doing wrong?
<?php
declare(strict_types=1);
class PageDisplay {
private $simplePageNav = true;
private $header = "";
private $pageTitle;
private $page;
private $csrfFlag;
private $pageType;
// Sets the variables the rest of the class will use
public function __contruct(string $pageTitle, string $page, bool $csrfFlag, string $pageType) {
$this->pageTitle = $pageTitle;
$this->page = $page;
$this->csrfFlag = $csrfFlag;
// Validating page type passed
switch ($pageType) {
case "main":
case "profile":
case "admin":
$this->pageType = $pageType;
default:
throw new Exception("A page type variable was passed that is unknown. That variable was $pageType");
}
}
public function getPage() : string {
return $this->page;
}
// If the function returns true, it is just a simple navigation to the page
public static function isPageNav(bool $getAllowed) : bool {
// Checking if GET parameters are allowed, then checking if the correct things are empty, then return boolean with what we find
if (!$getAllowed) {
$simplePageNav = (empty($_POST) && empty($_GET) && empty(file_get_contents("php://input"))) ? true : false;
} else {
$simplePageNav = (empty($_POST) && empty(file_get_contents("php://input"))) ? true : false;
}
return $simplePageNav;
}
// Gets what the navigation should be based on what type of page the user went to (general web, profile, admin, etc...)
private function getHeader() : string {
// Control statement to display website correctly
switch($this->pageType) {
case "auth_page":
return "";
break;
}
return "hard";
}
// Gets what the additional head tags should be based on what type of page the user went to (general web, profile, admin, etc...)
private function getHeadTags() : string {
// Control statement to display website correctly
switch($this->pageType) {
//
}
}
// Ultimately builds the page to show the user
public function buildPage(bool $needsHeadTags) : void {
// Generate CSRF token if needed
if ($this->csrfFlag === true) { $csrfToken = hash("sha512", random_bytes(64)); }
// Get extra HTML
if ($needsHeadTags === true) { $extraHeadTags = $this->getHeadTags(); }
$headerHtml = $this->getHeader();
$pageTitle = $this->pageTitle;
// Show page
include_once $this->page;
}
}
Upvotes: 0
Views: 316
Reputation: 9530
PHP has a number of "special" methods that can be added to classes that get automatically called at points during the class lifecycle. The __construct
function is executed on object initialisation, and __destruct
on object destruction, when there are no remaining references to the object or when the script or program ends. It's totally legal to create other class functions that also start with a double underscore, so PHP will not complain about this:
class PageDisplay {
// Sets the variables the rest of the class will use
public function __contruct(string $pageTitle, string $page, bool $csrfFlag, string $pageType) {
// code not executed unless $object->__contruct is called
}
}
but the code in the __contruct
function will not be executed when a new PageDisplay
object is created.
The fix is simple:
class PageDisplay {
// Sets the variables the rest of the class will use
public function __construct(string $pageTitle, string $page, bool $csrfFlag, string $pageType) {
// code executed on calling new PageDisplay('...')
}
}
Upvotes: 2