PHP类未设置我的变量之一

时间:2018-08-07 05:52:38

标签: php oop

我有一个用于显示页面的类,并且在构造函数中设置了许多类范围的变量。即使在调用创建对象的调用中可以看到传递参数的情况下,其中之一仍为null。这是该类的构造函数:

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");
    }

}

这是特定的对象创建调用:

$display = new PageDisplay('Login', 'login.php', true, 'auth_page');

我遇到的问题是标记为$ page的变量没有被传递(而且我知道这是因为我尝试在显示页面的类中以及当涉及显示行时调用了Late函数实际页面(在这种情况下为login.php)会给我这个错误:
Failed opening '' for inclusion)。
如果您想看到它,则是该函数: / p>

// 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

} 


但是我可以看到它是在构造函数中传递的。 我在做什么错了?

<?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;

    }


}

1 个答案:

答案 0 :(得分:2)

PHP有许多“特殊”方法,可以将它们添加到类中,这些方法会在类生命周期内的各个点被自动调用。当没有剩余的对象引用或脚本或程序结束时,__construct函数在对象初始化时执行,__destruct在对象破坏时执行。创建其他以双下划线开头的类函数完全合法,因此PHP不会对此抱怨:

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
    }
}

但是创建新的__contruct对象时,将不会执行PageDisplay函数中的代码。

解决方法很简单:

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('...')
    }
}