brigo brigo - 25 days ago 9
PHP Question

Cookie check in 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!

Answer

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.