douzhang6496 2016-11-14 17:19
浏览 37
已采纳

Cookie检查OOP

Until yesterday I was burning my brain trying to switch from a procedural thinking to a OOP thinking; this morning I gave up. I said to my self I wasn't probably ready yet to understand it.

I started then coding in the usual way, writing a function to check if there's the cookie "logged" or not

function chkCookieLogin() {
if(isset($_COOKIE["logged"])) {
    $logged = 'true';
    $cookieValue = $_COOKIE["logged"];

    return $logged;
    return $cookieValue;
}
else {
    $logged = 'false';

    return $logged;
}
}

$result = chkCookieLogin();
if($result == 'true'){
    echo $cookieValue;
}
else {
    echo 'NO COOKIE';
}

since I run across a problem: I wanted to return two variables ($logged and $cookieValue) instead of just one. I google it and I found this answer where Jasper explains a method using an OOP point of view (or this is what I can see). That answer opened me a new vision on the OOP so I tried to rewrite what I was trying to achieve this way:

class chkCookie {
public $logged;
public $cookieValue;

public function __construct($logged, $cookieValue) {
    $this->logged = $logged;
    $this->cookieValue = $cookieValue;
}

function chkCookieLogin() {
    $out = new chkCookie();
    if(isset($_COOKIE["logged"])) {
        $out->logged = 'true';
        $out->cookieValue = $_COOKIE["logged"];

        return $out;
    }
    else {
        $out->logged = 'false';

        return $out;
    }
}
}

$vars = chkCookieLogin();
$logged = $vars->logged;
$cookieValue = $vars->cookieValue;
echo $logged; echo $cookieValue;

Obviously it didn't work at the first attempt...and neither at the second and the third. But for the first time I feel I'm at one step to "really touch" the OOP (or this is what I think!).

My questions are:

  1. is this attempt correctly written from the OOP point of view?
  2. If yes, what are the problems? ('cause I guess there's more than one)

Thank you so much!

  • 写回答

1条回答 默认 最新

  • duanjiao3754 2016-11-14 17:35
    关注

    Credit to @NiettheDarkAbsol for the idea of returning an Array data-type.


    Using dependency injection, you can set-up an object like this:

    class Factory {
        private $Data = [];
        public function set($index, $data) {
            $this->Data[$index] = $data;
        }
    
        public function get($index) {
            return $this->Data[$index];
        }
    }
    

    Then to use the DI module, you can set methods like so (using anonymous functions):

    $f = new Factory();
    $f->set('Cookies', $_SESSION);
    $f->set('Check-Cookie', function() use ($f) {
        return $f->get('Cookies')['logged'] ? [true, $f->get('Cookies')['logged']] : [false, null];
    });
    

    Using error checks, we can then call the method when and as we need it:

    $cookieArr = is_callable($f->get('Check-Cookie')) ? call_user_func($f->get('Check-Cookie')) : [];
    echo $cookieArr[0] ? $cookieArr[1] : 'Logged is not set';
    

    I'd also consider adding constants to your DI class, allowing more dynamic approaches rather than doing error checks each time. IE, on set() include a constant like Factory::FUNC_ARRAY so your get() method can return the closure already executed.

    You can look into using ternary operators if you're confused.

    See it working over at 3v4l.org.
    If it means anything, here is an OOP styled approach.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考