dongqi3533 2013-12-21 18:44
浏览 29
已采纳

PHP获取相应的数据,默认和错误处理

I have a normal HTML menu that passes a GET statement to the url.

<li><a href="?Page=Home">Home</a></li>

Just like this, al though this is ofcourse only 1 item of the entire menu. In a seperated file I have an function that checks if an GET or POST statement exist, and If it exist and is not NULL then it will give the value back to where it was called.

public function getFormVariable($value){

    switch (strtoupper($_SERVER['REQUEST_METHOD'])) {
        case 'GET':
            if (isset($_GET[$value]) && $_GET[$value] != NULL) {
                return $_GET[$value];
            }
            else{
                return false;
            }
            break;
            case 'POST':
                if (isset($POST[$value]) && $POST[$value] != NULL) {
                return $POST[$value];
            }
            else{
                    return false;
            }
                break;

        default:
            return false;

    }
}

And with the following code it takes the get value and finds the corrosponding class (every class is in a seperated file, and every class is 1 link in my menu)

In this class there is just some regular functions/data that gives the content of that page.

$class = loadClass($ConfigPage->getFormVariable('Page'));
        $ConfigPage->SetProperty('content', $class);

        function loadClass($Page){
            $class_name = 'Content' . $Page;

            if(!class_exists($class_name)){
                return 'Error: Content has not been found.';
            }

            $class = new $class_name();
            return $class;
        }

Explaining: The menu gives a GET value of 'Contact' which is then check by GetFormVariable() and then the corresponding class is found which gives back the content that class holds.

Now my question: When the function LoadClass() cant find the name of the class it was given through the GET statement, it should return a error string. But this is not happening. I get a beautiful big orange error from PHP saying the following:

Fatal error: Call to a member function render() on a non-object in E:\Program files\wamp\www\BDW\Class\Html_Page.Class.php on line 147

Line 147 is where to object is called

echo $this->content->render();

The Render function is as it says a normal return function inside the content classes. Why do i get this error, and how do i fix it?

Second question. If there is no GET statement in the url. It gives the exact same error. which is pretty logical. But how do i make it show ContentHome when there is no GET statement in the url, and an ERROR when the value of the GET statement is incorrect.

Thank you for reading, If there is anything unclear please tell me. English is not my native language, and after all. I am here to learn.

EDIT: My knowledge of PHP is not great enough, so i decided when a class can not be found. it brings you back to home without any error. which i wanted in the first place.

Here is my new code which is still not working, why?

$class = loadClass($ConfigPage->getFormVariable('Page'));
        $ConfigPage->SetProperty('content', $class);

        function loadClass($Page){
            $class_name = 'Content' . $Page;
            $Default_Class = 'ContentHome';

            if(!class_exists($class_name)){
                //echo 'Error: Content has not been found.';
                $class = new $Default_Class();
                return $class;
            }
            else{
                $class = new $class_name();
                return $class;
            }

            $ConfigPage->Render();
        }
  • 写回答

1条回答 默认 最新

  • douzhao7445 2013-12-21 18:50
    关注

    It's happening because of this line :

            if(!class_exists($class_name)){
                return 'Error: Content has not been found.';
            }
    

    In the case the class doesn't exist, you're returning a string from the function, and afterwards trying to call a method render() on it. You can fix it either by changing this behaviour (don't return an error string, but use an Exception or trigger_error() ) or checking that the function loadClass() does return a valid object through is_object() for example.

    Regarding your second question, you should expand your tests in loadClass() to handle the empty GET variable case, and substitute the empty string with a "Home" default value.


    Update :

    Example usage for is_object in your case :

    $class = loadClass($ConfigPage->getFormVariable('Page'));
    if(! is_object($class)) {
        // if $class is not an object, then something went wrong above
        throw new \Exception('Invalid data returned from loadClass()');
    }
    $ConfigPage->SetProperty('content', $class);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作