2

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!

Community
  • 1
  • 1
Brigo
  • 1,086
  • 1
  • 12
  • 36
  • 3
    That's a horrible way of doing it... Go back to procedural for this, and note that you can `return array("logged" => $logged, "cookieValue" => $cookieValue);` – Niet the Dark Absol Nov 14 '16 at 17:23
  • 1
    I think that the friends on codereview can help you better on this one, https://codereview.stackexchange.com/ – Federkun Nov 14 '16 at 17:25
  • @Niet the Dark Absol End but apart for the specific case of the cookie check, did I go close to the OOP thinking or not? 'Cause for me try to switch from one to another method has the same feeling of walking blindfolded in a dark room: I've 0 certainities! – Brigo Nov 14 '16 at 17:33
  • @Federkun thanks for the tip! I even didn't know about its existence! – Brigo Nov 14 '16 at 17:34

1 Answers1

3

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.

Community
  • 1
  • 1
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • Ok, when I grow up I wanna be like you. It's so different and ways more complicated compared to the "(bad) style" I code now that I feel like a baby! I'm gonna study your script word by word to get deep into it 'cause it's fascinating – Brigo Nov 14 '16 at 17:54
  • Haha, glad I could help. @brigo – Jaquarh Nov 14 '16 at 17:59
  • I'm not sure why this is a good answer. The `Factory` class is just an array here. Just because there's the word "class" somewhere doesn't mean that this isn't a procedural code. – Federkun Nov 14 '16 at 19:05
  • Don't ruin his vibe ;) here is an [OOP approach](https://3v4l.org/et9v9) if it suits better @Federkun – Jaquarh Nov 14 '16 at 19:27