Passing Boolean to a Function

This is a discussion on Passing Boolean to a Function within the PHP Language forums, part of the PHP Programming Forums category; I'm having an issue passing a boolean to the constructor of an object. For some reason, if I pass ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-10-2006
Greg Scharlemann
 
Posts: n/a
Default Passing Boolean to a Function

I'm having an issue passing a boolean to the constructor of an object.
For some reason, if I pass false into the constructor, it doesn't
register. If I pass an integer 0, it does. PHP 5 Code below:

---------------------
class Test {
var $testBool;

public function __construct($testBool) {
print "testBool = $testBool";
}
}

$testing = new Test(false); // this just prints "testBool = "

$testing = new Test(0); // this prints "testBool = 0"

--------------------
Seems to be both methods should print 0 or false. What have I missed?

Reply With Quote
  #2 (permalink)  
Old 07-10-2006
flamer die.spam@hotmail.com
 
Posts: n/a
Default Re: Passing Boolean to a Function

hmm I think you may need to use 'null' rather than false.

Flamer.


Greg Scharlemann wrote:

> I'm having an issue passing a boolean to the constructor of an object.
> For some reason, if I pass false into the constructor, it doesn't
> register. If I pass an integer 0, it does. PHP 5 Code below:
>
> ---------------------
> class Test {
> var $testBool;
>
> public function __construct($testBool) {
> print "testBool = $testBool";
> }
> }
>
> $testing = new Test(false); // this just prints "testBool = "
>
> $testing = new Test(0); // this prints "testBool = 0"
>
> --------------------
> Seems to be both methods should print 0 or false. What have I missed?


Reply With Quote
  #3 (permalink)  
Old 07-10-2006
David Haynes
 
Posts: n/a
Default Re: Passing Boolean to a Function

Greg Scharlemann wrote:
> I'm having an issue passing a boolean to the constructor of an object.
> For some reason, if I pass false into the constructor, it doesn't
> register. If I pass an integer 0, it does. PHP 5 Code below:
>
> ---------------------
> class Test {
> var $testBool;
>
> public function __construct($testBool) {
> print "testBool = $testBool";
> }
> }
>
> $testing = new Test(false); // this just prints "testBool = "
>
> $testing = new Test(0); // this prints "testBool = 0"
>
> --------------------
> Seems to be both methods should print 0 or false. What have I missed?
>


What are you expecting the print of true and false to produce?
Also, var is deprecated in PHP5.

Try this class to see what is really happening.
<?php
class Test {
private $testBool;

public function __construct($testBool) {
printf("Test constructed with testBool = %s\n", $testBool ? 'true' :
'false');
}
}

$testing = new Test(true);
$testing = new Test(false);
?>

-david-

Reply With Quote
  #4 (permalink)  
Old 07-10-2006
Chung Leong
 
Posts: n/a
Default Re: Passing Boolean to a Function

Greg Scharlemann wrote:
> Seems to be both methods should print 0 or false. What have I missed?


The fact that false becomes an empty string when casted to string.

For debugging it's better to use var_dump($var).

Reply With Quote
  #5 (permalink)  
Old 07-10-2006
Kimmo Laine
 
Posts: n/a
Default Attention Flamer! (Was: Passing Boolean to a Function)

<die.spam@hotmail.com> wrote in message
news:1152490344.038778.285330@m79g2000cwm.googlegr oups.com...
> hmm I think you may need to use 'null' rather than false.
>



Flamer

I'm sure everyone here appreciates your enthusiasm, but on more than one
occasions I've noticed that your answers have very little or nothing to do
with the questions. I don't mean to be offensive, but if you really don't
know what the answer is, it would be best not to answer, rather than guess
something. Not answering is better than confusing even more the person who
is asking something. For example in this case using 'null' isn't going to
solve anything, it's a case of how booleans are cast to strings compared to
how integers are.

I don't want you to stop posting here, what I'd like you to do is think for
a while before each posting, is this answer really helpful, accurate, and
more than a hunch. If not, don't worry, someone else may have the right
answer. We're not here to compete who answers the most posts fastest, but to
really help people who have difficulties and need help (that is: not random
guessing) and I wish you and everyone else here realizes that.

--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)


Reply With Quote
  #6 (permalink)  
Old 07-10-2006
Chung Leong
 
Posts: n/a
Default Re: Attention Flamer! (Was: Passing Boolean to a Function)

Kimmo Laine wrote:
> <die.spam@hotmail.com> wrote in message
> news:1152490344.038778.285330@m79g2000cwm.googlegr oups.com...
> > hmm I think you may need to use 'null' rather than false.
> >

>
>
> Flamer
>
> I'm sure everyone here appreciates your enthusiasm, but on more than one
> occasions I've noticed that your answers have very little or nothing to do
> with the questions. I don't mean to be offensive, but if you really don't
> know what the answer is, it would be best not to answer, rather than guess
> something. Not answering is better than confusing even more the person who
> is asking something. For example in this case using 'null' isn't going to
> solve anything, it's a case of how booleans are cast to strings compared to
> how integers are.
>
> I don't want you to stop posting here, what I'd like you to do is think for
> a while before each posting, is this answer really helpful, accurate, and
> more than a hunch. If not, don't worry, someone else may have the right
> answer. We're not here to compete who answers the most posts fastest, but to
> really help people who have difficulties and need help (that is: not random
> guessing) and I wish you and everyone else here realizes that.
>
> --
> "ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
> spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)


One thing I have noticed is that Diet Dr. Pepper really doesn't taste
like regular Dr. Pepper.

Reply With Quote
  #7 (permalink)  
Old 07-10-2006
Greg Scharlemann
 
Posts: n/a
Default Re: Passing Boolean to a Function

David Haynes wrote:
> Also, var is deprecated in PHP5.

Thanks!
Your test case also worked... but I'm going to make my question a
little more complicated...

That boolean value is going to be written to a MySQL database. I've set
the column to be of type Bool (which appears to turn into a
Tinyint(1)), is that the best approach? Should I use enum('true',
'false') or some other approach?

Second, since the values get screwed up when cast to a String, should I
just go around the true/false nominclature and use 1/0?

Finally, another somewhat semi-related newbie question. I come from a
java background with getter and setter methods. I modified the Test
class David created with what I thought is a getter method, however it
doesn't work... The function just prints: "()" PHP is not as straight
forward as I hoped...

<?php
class Test {
private $testBool;

public function __construct($testBool) {
printf("Test constructed with testBool = %s\n",
$testBool ? 'true' :
'false');
}

public function getTestBool() {
return "$testBool";
}

}
$testing = new Test(true);
$testing = new Test(false);
print "testing->getTestBool() = $testing->getTestBool()";
?>

Reply With Quote
  #8 (permalink)  
Old 07-10-2006
David Haynes
 
Posts: n/a
Default Re: Passing Boolean to a Function

Greg Scharlemann wrote:
> David Haynes wrote:
>> Also, var is deprecated in PHP5.

> Thanks!
> Your test case also worked... but I'm going to make my question a
> little more complicated...
>
> That boolean value is going to be written to a MySQL database. I've set
> the column to be of type Bool (which appears to turn into a
> Tinyint(1)), is that the best approach? Should I use enum('true',
> 'false') or some other approach?


I would translate the 0/1 from MySQL into true/false at the lowest level
I could. It's not all that hard to unroll it as follows:

$result = mysqli_query($sql);
while( $row = mysql_fetch_assoc($result) ) {
$isArchived = $row['is_archived'] ? true : false;
...
}

NOTE: Since you are familiar with Java, the use of isXxx should be very
familiar to you.

> Second, since the values get screwed up when cast to a String, should I
> just go around the true/false nominclature and use 1/0?


There are at least two ways to handle this:
1. create your own class to handle the 1/0 to true/false mapping
2. provide a toString() method for the boolean value in a class
>
> Finally, another somewhat semi-related newbie question. I come from a
> java background with getter and setter methods. I modified the Test
> class David created with what I thought is a getter method, however it
> doesn't work... The function just prints: "()" PHP is not as straight
> forward as I hoped...
>
> <?php
> class Test {
> private $testBool;
>
> public function __construct($testBool) {
> printf("Test constructed with testBool = %s\n",
> $testBool ? 'true' :
> 'false');
> }
>
> public function getTestBool() {
> return "$testBool";
> }
>
> }
> $testing = new Test(true);
> $testing = new Test(false);
> print "testing->getTestBool() = $testing->getTestBool()";
> ?>


Getters and setters (or more formally, accessors and mutators) are fully
supported in PHP. What you have here is another example of trying to
print FALSE as a string (which maps to the null string).

So, let's play with the code a bit...
<?php
class Test {
// holds the boolean value - could be protected instead
private $testBool;

// constructor
public function __construct($testBool) {
$this->testBool = $testBool;
}

// accessor
public function getTestBool() {
return $this->testBool;
}

// mutator
public function setTestBool($testBool) {
$this->testBool = $testBool;
}

// toString
public function toString() {
return $this->testBool ? 'true' : 'false';
}

// isTrue
public function isTrue() {
return ($this->testBool == true);
}
}

// constructor
$my_test = new Test(true);

// accessor
if( $my_test->getTestBool() == true ) {
printf("getTestBool returned true\n");
}

// mutator
$my_test->setTestBool(false);

// toString
printf('testBool is %s\n', $my_test->toString());

// isTrue
if( $my_test->isTrue() ) {
printf('isTrue returned true\n');
} else {
printf('isTrue returned false\n');
}
?>

You can add your own toMySQL() and setTestBoolFromMySQL() if you want.

-david-

Reply With Quote
  #9 (permalink)  
Old 07-11-2006
Greg Scharlemann
 
Posts: n/a
Default Re: Passing Boolean to a Function


> There are at least two ways to handle this:
> 1. create your own class to handle the 1/0 to true/false mapping
> 2. provide a toString() method for the boolean value in a class

I like the toString() method approach. Good call.

> So, let's play with the code a bit...

Great example! Thank you so much for your help.

Reply With Quote
  #10 (permalink)  
Old 07-11-2006
Richard Levasseur
 
Posts: n/a
Default Re: Attention Flamer! (Was: Passing Boolean to a Function)


Kimmo Laine wrote:
> <die.spam@hotmail.com> wrote in message
> news:1152490344.038778.285330@m79g2000cwm.googlegr oups.com...
> > hmm I think you may need to use 'null' rather than false.
> >

>
>
> Flamer
>
> I'm sure everyone here appreciates your enthusiasm, but on more than one
> occasions I've noticed that your answers have very little or nothing to do
> with the questions. I don't mean to be offensive, but if you really don't
> know what the answer is, it would be best not to answer, rather than guess
> something. Not answering is better than confusing even more the person who
> is asking something. For example in this case using 'null' isn't going to
> solve anything, it's a case of how booleans are cast to strings compared to
> how integers are.
>
> I don't want you to stop posting here, what I'd like you to do is think for
> a while before each posting, is this answer really helpful, accurate, and
> more than a hunch. If not, don't worry, someone else may have the right
> answer. We're not here to compete who answers the most posts fastest, but to
> really help people who have difficulties and need help (that is: not random
> guessing) and I wish you and everyone else here realizes that.
>
> --
> "ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
> spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)



Ohhh, burned.

I'll be here all night, folks.

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:47 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0