PHP获取相应的数据,具有默认和错误处理 [英] PHP Get corresponding data, with default and error handling

查看:150
本文介绍了PHP获取相应的数据,具有默认和错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正常的HTML菜单,将GET语句传递到网址。

 < li>< a href =?第页=首页>首页< / a>< / li> 

就像这样,虽然这只是整个菜单的一个项目。
在一个单独的文件中,我有一个函数检查是否存在GET或POST语句,
如果它存在并且不为NULL,那么它将返回调用它的值。

  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;

默认值:
return false;

}
}

获取值并找到腐化类
(每个类都在一个单独的文件中,每个类在我的菜单中是1个链接)



在这个类中只是一些常规函数/数据,给出该页面的内容。

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

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

if(!class_exists($ class_name)){
return'错误:未找到内容。
}

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

解释:菜单给出一个GET值为'Contact'



现在我的问题:
当函数LoadClass()可以使用的时候,可以使用GetFormVariable找到通过GET语句给出的类的名称,它应该返回一个错误字符串。但这不是发生。我从PHP得到一个美丽的大橙色错误说:以下:


致命错误:调用一个成员函数render对象
E:\Program files\wamp\www\BDW\Class\Html_Page.Class.php在第147行上


147行是对象被调用的地方


echo $ this-> content-> render / p>

Render函数与内容类中的正常返回函数一样。
为什么我得到这个错误,我如何解决它?



第二个问题。如果在url中没有GET语句。它给出完全相同的错误。这是很合理的。但是如何让它显示ContentHome当没有GET语句在url,和GET语句的值不正确的错误。



谢谢你阅读,
如果有什么不清楚请告诉我。英语不是我的母语,毕竟。我在这里学习。



编辑:
我对PHP的了解不够多,所以我决定当一个类无法找到。它带给你回家没有任何错误。



<$>这是我的新代码仍然不工作,为什么?



<$> p $ p> $ 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'错误:未找到内容。
$ class = new $ Default_Class();
return $ class;
}
else {
$ class = new $ class_name();
return $ class;
}

$ ConfigPage-> Render();
}


解决方案

  if(!class_exists($ class_name)){
return'错误:未找到内容。 ;
}

在类不存在的情况下,从函数,然后尝试调用方法render()。您可以通过更改此行为(不返回错误字符串,但使用异常或trigger_error())或检查函数loadClass()通过is_object()返回有效对象来修复它。



关于你的第二个问题,你应该在loadClass()中扩展你的测试来处理空的GET变量case,并用空字符串替换为Home默认值。






更新:



is_object in your case:

  $ class = loadClass($ ConfigPage-> getFormVariable('Page')); 
if(!is_object($ class)){
//如果$ class不是一个对象,那么在
上出现错误throw new \Exception('Invalid data returned from loadClass )');
}
$ ConfigPage-> SetProperty('content',$ class);


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

解决方案

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

这篇关于PHP获取相应的数据,具有默认和错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆