1

我有这段代码来呈现模板(我是 PHP 新手,所以请耐心等待)

function __destruct()
    {
        /*
        * It's render the template 
        *
        */
            $this->_template->render();
    }

出现此错误:

Fatal error: Call to a member function render() on a non-object in /opt/lampp/htdocs/soap/php/library/controller.class.php on line 73

这是构造

function __construct($model, $controller, $action) 
    {
        /*
        * A default constructor which creates objects of the template and model
        * @param string model, controller and action.
        * @return
        */

        $this->_controller = $controller;
        $this->_action = $action;
        $this->_model = $model;

        $this->$model = new $model;
        $this->_template = new Template($controller,$action);

    }

如果您认为我应该添加更多内容,请告诉我。我是 PHP 新手,正在尝试让这个应用程序正常工作。欢迎任何想法!

更新:全班

<?php
/*
*
* controller.class.php - Controller class will be used as the base class 
*                        for all our controllers
*
*
*   The above class is used for all the communication between the controller, 
*   the model and the view (template class). It creates an object for the 
*   model class and an object for template class. The object for model class 
*   has the same name as the model itself
*
*
* We are not including the ?\>  to avoid injection of any extra whitespaces in our output.
*
*
*/
class Controller extends Validation
{

    protected $_model;
    protected $_controller;
    protected $_action;
    protected $_template;


// ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*


    function __construct($model, $controller, $action) 
    {
        /*
        * A default constructor which creates objects of the template and model
        * @param string model, controller and action.
        * @return
        */

        $this->_controller = $controller;
        $this->_action = $action;
        $this->_model = $model;

        $this->$model = new $model;
        $this->_template = new Template($controller,$action);

    }


// ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*


    function set($name,$value)
    {
       /*
        * Set the variables from the controller to the template as a variables.
        * @param string name,value
        * @return
        */
        $this->_template->set($name,$value);
    }


// ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*


    function __destruct()
    {
        /*
        * It's render the template 
        *
        */
            $this->_template->render();
    }


}

验证类:

<?php
/*
*
* pdoconnectionclass.php - Validation is used to validate any input data to the website
*
*
*   The above class is used for checking every data that are the user wrote it and are going
*   to be stored in the database. For each type of input there is an appropriate function 
*   to check it before is going to be store in the database.
*
*
* We are not including the ?\>  to avoid injection of any extra whitespaces in our output.
*
*
*/

    class Validation
    {

        public function isEmpty($check_var) 
        {
        /** Check if the input value is empty or not
        *   If is empty then return true back on the main program
        *   else return false
        */

            if (empty($check_var)){
                return true;
            }
            else
            {
                return false;

            }

        }


        public function isInt($check_var) 
        {
        /** Check if the input value is integer
        *   and if Yes then return false else if not and is not int return false
        */
            if (is_int($check_var)) {
                return true;
            }
            else
            {
                return false;

            }

        }

        public function isPositive($check_var) 
        {
        /** Check if the input value is positive
        *   and if Yes then return false else if not and is not int return false
        */
            if ($check_var > 0) 
            {
                return true;
            }
            else
            {
                return false;

            }

        }

        public function checking_phone($check_var) 
        {
        /** Check if the input value is a phone
            Check if the if the characters of $check_var
            are equal to 14 if the value has 0044 or if are 11
            if they start from 07. Then check if is positive
            and then removes the 0 from 07 and adds 44 at the beginning
        */
            if (strlen($check_var) == 11)
            {
                if ($this->isPositive($check_var) == true)
                {
                    if (($check_var >= '07000000000') && ($check_var <= '07999999999'))
                    {
                        $temp_phone = "44";
                        $temp_phone .= substr($check_var, 1);
                        return $temp_phone;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            else if (strlen($check_var) == 14)
            {
                if ($this->isPositive($check_var) == true)
                {
                    if (($check_var >= '00447000000000') && ($check_var <= '00447999999999'))
                    {
                        $temp_phone = substr($check_var, 2);
                        return $temp_phone;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }

            }
            else if (strlen($check_var) == 12)
            {
                if ($this->isPositive($check_var) == true)
                {
                    if (($check_var >= '447000000000') && ($check_var <= '447999999999'))
                    {
                        return $check_var;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }

            }
            else
            {
                return false;
            }

        }


        public function checkforXSS($check_var) 
        {
        /** Gets the input value and prevents HTML from being embedded, 
            and therefore prevents JavaScript embedding as well. 
            Return a safe versions of the input.    
            The htmlentities() function converts both double and single
            quotation marks to entities and the text to ASCII compatible multi-byte 8-bit Unicode.
        */
            htmlentities( $check_var, ENT_QUOTES, 'utf-8' );
            return $check_var;
        }



        public function filter_var_int($var)
        {
        /** Filters a variable with the specified filter.
        */
            return filter_var($var, FILTER_SANITIZE_NUMBER_INT);
        }


        public function filter_var_string($var)
        {
        /** Filters a variable with the specified filter.
        */
            return filter_var($var, FILTER_SANITIZE_STRING);
        }


        public function filter_var_email($var)
        {
        /** Filters a variable with the specified filter.
        */
            $temp = '';
            $temp = filter_var($var, FILTER_SANITIZE_EMAIL);
            $temp = filter_var($temp, FILTER_VALIDATE_EMAIL);
            return $temp;
        }       

        public function create_salt_password($username)
        {
        /** Creates a hash value for the password using 
            a prefixed random unique identifier value with a static characters and the username
        */
            $salt = hash('sha256', uniqid(mt_rand(), true) .AUTH_SALT .strtolower($username));
            return $salt;
        }

        public function get_activation_code()
        {
        /** Creates an activation code for the user
        */
            return md5(uniqid(rand(), true));;
        }

    }
4

1 回答 1

0

来自 PHP 文档:

只要没有其他对特定对象的引用,或者在关闭序列期间以任何顺序调用,就会调用析构函数方法。

请注意,当析构函数由于脚本结束而运行时,它们可能以任何顺序运行,这可能会给您留下对已经被析构的对象的引用。它没有明确记录,但这意味着您无法从析构函数安全地访问其他对象,因为它们可能不再存在。

这里可能发生的是Template对象首先被销毁,当您尝试从Controller析构函数访问它时导致错误。这也可以解释为什么它适用于某些机器而不适用于其他机器——因为破坏顺序是未定义的,并且在不同机器之间可能会有很大差异。

这个问题似乎没有明确记录,但根据这个随机页面,你不是第一个遇到这个问题的人:

...或者当到达脚本末尾并且 PHP 结束请求时。后一种情况很微妙,因为您依赖于一些可能已经调用了析构函数并且不再可访问的对象。因此,请谨慎使用它,不要依赖析构函数中的其他对象。


所有这一切的真正解决方案是在你的析构函数中没有这种类型的功能。在脚本末尾手动调用方法之类的方法会更干净renderHTML(),而不是自动发生一些隐藏的行为。

有关更多详细信息,请参阅这些其他问题:

如果您真的需要它自动发生,我建议您register_shutdown_function()改为查看。您可以将Controller析构函数更改为名为类似的常规方法renderHTML,并通过将此行放入构造函数中注册以使该方法在关闭时自动运行:

register_shutdown_function(array($this, 'renderHTML'));

其中array()有一种方法可以在 PHP中指定回调方法。

于 2013-07-12T22:47:31.290 回答