WatsMyName
WatsMyName

Reputation: 4478

Strange behavior of isset and empty function

I am running into strange problem. I have following simple line in PHP

<?php
    echo "Value = ".$this->language; //outputs Value = en
    echo "<br>isset = ".isset($this->language);  //Outputs isset = 
    echo "<br>Is empty = ".empty($this->language);  //Outputs Is empty= 1 
?>

Why is that the second line doesn't prints true or '1' and third line prints its empty, when its clear from first line that this->language is set and is not empty??

Upvotes: 0

Views: 86

Answers (4)

Dinesh Belkare
Dinesh Belkare

Reputation: 631

Explanation for isset() is as below.

isset($this->language) will always echo 'false'. because the isset() accepts VARIABLES as it's parameters, but in this case, $this->language is NOT a VARIABLE. it is a VALUE returned from the __get() method of the class. Thus the isset($this->language) expression will always equal 'false'. Please refer http://php.net/manual/en/function.isset.php

Explanation for empty() is as below.

class Registry
{
    //Definition goes here
}
$registry = new Registry();
$registry->empty = '';
$registry->notEmpty = 'not empty';

var_dump(empty($registry->empty)); // true, so far so good
var_dump(empty($registry->notEmpty)); // true, .. say what?
$tmp = $registry->notEmpty;
var_dump(empty($tmp)); // false as expected

The result for empty($registry->notEmpty) is a bit unexpected as the value is obviously set and non-empty. This is due to the fact that the empty() function uses __isset() magic function in these cases. Please refer http://php.net/manual/en/function.empty.php

Upvotes: 1

WatsMyName
WatsMyName

Reputation: 4478

Actually, the code is very complex, the code I was working on with is built with Silverstripe. But i found a dirty workaround that will work on plain PHP as well.

in parent class, i created a method which checks if language is set, like this

public function isLanguageEmpty(){
        return isset($this->language);
    }

and in child class we can simply use $this->isLanguageEmpty()

But to understand what might be the actual problem, the comments in original post and the other answers will be helpful.

Upvotes: 0

Naveen Kumar
Naveen Kumar

Reputation: 4601

Below Code Snippet should help you

If the Variable is declared as private in parent class it will not be available in the child class , it has to be either protected or private

class base {
   // changed to protected
   protected $language = 'en';
}

class child extends base {

  public function spit(){
     echo "Value = ".$this->language; //outputs Value = en
     echo "<br>isset = ".isset($this->language);  //Outputs isset = 
     echo "<br>Is empty = ".empty($this->language);  //Outputs Is empty= 1 
   }
}
$ch = new child();
$ch->spit();

Upvotes: 1

Dipanwita Kundu
Dipanwita Kundu

Reputation: 1667

instead of 'echo' use var_dump.Will help you to understand the difference.

$language = 'en';
echo "Value = ".$language; //outputs Value = en
$isset = isset($language);
$empty = empty($language);
echo "<br>isset = ";;  
var_dump($isset); //bool(true)
echo "<br>Is empty = ";  
var_dump($empty); // bool(false)

Upvotes: 1

Related Questions