php - Unexpected output on a ANDING boolean expression in a class and a single if statement -
i wrote function getting data out of array. part in function verification of data, checks if key not empty , if array key not exists (my bad after edit). function looks this:
public function getdata($key = "") { if (!empty($key) && !array_key_exists($key, $this->data)) { throw new configurationexception("$key not exist"); } return empty($key) ? $this->data : $this->data[$key]; }
after applying srp princple, putting 'verification' class, looked this:
class configurationverificationhandler { public function ispresent($key, array $data) { return empty($key) && array_key_exists($key, $data); } } public function getdata($key = "") { $verificationhandler = new configurationverificationhandler(); if (!$verificationhandler->ispresent($key, $this->data)) { throw new configurationexception("$key not exist"); } return empty($key) ? $this->data : $this->data[$key]; }
from both of these snippets, expected same result, because put !
operator before ispresent
function, making check if condition false.
how come boolean expressions not same , dont give same result?
!a && !b
not same !(a && b)
. 1 has @ case 1 variable true (let's pick a) , other false:
!true && !false --> false && true --> false !(true && false) --> !(false) --> true
however, !a && !b
is equivalent !(a || b)
:
!(true || false) --> !(true) --> false
Comments
Post a Comment