donmqryh49993 2013-12-21 20:46
浏览 66
已采纳

PHP在它自己的构造函数中创建对象

EDIT: Link to my first question. Might clear some things up. PHP Get corresponding data, with default and error handling

I have a function that checks if a GET statement exists. If so, it passes the value to a other function that then selects a class based on the value of the GET statement.

explaining: The url = Page=Contact
The GetFormVariable approves it, and the class Contact is selected and it will give back a string. This string is used as an object 'Content' that, as it says, creats the content of the page.

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;

    }
}

Now the question. When there is no GET statement in the url. The GetFormVariable returns false. And this means there is nothing shown.

How do i give this constructor.

public function SetProperty ($prob, $val){
    $this->$prob = $val;
}

The information to create the ContentHome.
SetProperty('Content', 'ContentHome');

Sorry for poor explanation, if anything is unclear please tell me so.

  • 写回答

2条回答 默认 最新

  • ds11111111111111111 2013-12-21 20:59
    关注

    I'm suggesting we close this question as unclear what you're asking, but decided to throw some help on the provided code sample anyway...

    You can strip this down dramatically, and since there's no context, the function can be static too.

    static public function getFormVariable($value)
    {
        if($_SERVER['REQUEST_METHOD'] == 'GET' &&
           isset($_GET[$value])                &&
           !empty($_GET[$value]))
            return $_GET[$value];
    
        elseif($_SERVER['REQUEST_METHOD'] == 'POST' &&
               isset($POST[$value])                 &&
               !empty($POST[$value]))
            return $POST[$value];
    
        return false;
    }
    

    Your original isset and != NULL checks were doing the same check. Maybe you want the empty() check as a third check, but look it up to be certain.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?