473,414 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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?

Jul 9 '06 #1
18 6001
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?
Jul 10 '06 #2
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-

Jul 10 '06 #3
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).

Jul 10 '06 #4
<di******@hotmail.comwrote in message
news:11**********************@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
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Jul 10 '06 #5
Kimmo Laine wrote:
<di******@hotmail.comwrote in message
news:11**********************@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
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
One thing I have noticed is that Diet Dr. Pepper really doesn't taste
like regular Dr. Pepper.

Jul 10 '06 #6
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()";
?>

Jul 10 '06 #7
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-

Jul 10 '06 #8
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.

Jul 10 '06 #9

Kimmo Laine wrote:
<di******@hotmail.comwrote in message
news:11**********************@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
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)

Ohhh, burned.

I'll be here all night, folks.

Jul 11 '06 #10
Rik
David Haynes wrote:
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;
...
}

$isArchived = (bool)$row['isarchived'];
it's shorter, and by using $row['is_archived'] ? you're already evaluating
the boolean value.
http://www.php.net/manual/en/languag...oolean.casting

Grtz,
--
Rik Wasmus
Jul 11 '06 #11
"Greg Scharlemann" <gr**************@gmail.comwrote in message
news:11*********************@75g2000cwc.googlegrou ps.com...
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...
print "testing->getTestBool() = $testing->getTestBool()";
?>
This is evaluated actually like this would be :
'testing->getTestBool() = '.$testing->getTestBool.'()';

and since you have not property getTestBool, $testing->getTestBool returns
nothing. therefore it's "testing->getTestBool() = ".[nothing]."()"; that's
why it prints only the empty parenthesis.

Functions are not executed within strings (you'd have to be using eval but
you don't want that). So you need to execute the getter method outside the
string:

print "testing->getTestBool() = " . $testing->getTestBool();

--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Jul 11 '06 #12
Rik wrote:
David Haynes wrote:
>$result = mysqli_query($sql);
while( $row = mysql_fetch_assoc($result) ) {
$isArchived = $row['is_archived'] ? true : false;
...
}


$isArchived = (bool)$row['isarchived'];
it's shorter, and by using $row['is_archived'] ? you're already evaluating
the boolean value.
http://www.php.net/manual/en/languag...oolean.casting

Grtz,
Rik,
$row['isarchived'] is already a 0/1 value (Tinyint(1)), so really the
(bool) cast is a no-op. My bad. You could just use $isArchived =
$row['isarchived']

-david-

Jul 11 '06 #13
Rik
David Haynes wrote:
Rik wrote:
>David Haynes wrote:
>>$result = mysqli_query($sql);
while( $row = mysql_fetch_assoc($result) ) {
$isArchived = $row['is_archived'] ? true : false;
...
}


$isArchived = (bool)$row['isarchived'];
it's shorter, and by using $row['is_archived'] ? you're already
evaluating
the boolean value.
http://www.php.net/manual/en/languag...oolean.casting
>>
Grtz,

Rik,
$row['isarchived'] is already a 0/1 value (Tinyint(1)), so really the
(bool) cast is a no-op. My bad. You could just use $isArchived =
$row['isarchived']
What do you mean by no-op? I'm no native speaker, please explain. And
already 0/1? 0/1 is an integer, not a boolean.

<?php
$row['test'] = 1; //so, integer 1, can be cast to true
$istrue = (bool)$row['test'];
echo '$row[test] can be cast to ';
echo ($row['test'])?'true':'false';
$string = "\n%s is %sa boolean";
printf($string,'$row[test]',($row['test']===true)? '' :'not ');
printf($string,'$istrue',($istrue===true)? '' :'not ');
?>

Grtz,
--
Rik Wasmus
Jul 11 '06 #14
Rik wrote:
David Haynes wrote:
>>Rik wrote:
>>>David Haynes wrote:

$result = mysqli_query($sql);
while( $row = mysql_fetch_assoc($result) ) {
$isArchived = $row['is_archived'] ? true : false;
...
}
$isArchived = (bool)$row['isarchived'];
it's shorter, and by using $row['is_archived'] ? you're already
evaluating
the boolean value.

http://www.php.net/manual/en/languag...oolean.casting
>>>Grtz,

Rik,
$row['isarchived'] is already a 0/1 value (Tinyint(1)), so really the
(bool) cast is a no-op. My bad. You could just use $isArchived =
$row['isarchived']


What do you mean by no-op? I'm no native speaker, please explain. And
already 0/1? 0/1 is an integer, not a boolean.

<?php
$row['test'] = 1; //so, integer 1, can be cast to true
$istrue = (bool)$row['test'];
echo '$row[test] can be cast to ';
echo ($row['test'])?'true':'false';
$string = "\n%s is %sa boolean";
printf($string,'$row[test]',($row['test']===true)? '' :'not ');
printf($string,'$istrue',($istrue===true)? '' :'not ');
?>

Grtz,
Rik,

No-Op is from Assembler - "No Operation", although it is more correctly
referred to as "noop". It's an instruction which does nothing but take
time and memory. Useful when you need to delay a couple of CPU cycles
for something else to occur.

And you are correct - the (bool) is needed if you want the value to be a
boolean. However, if you're only going to test it, you could leave it
as an integer 1/0 instead of true/false; the result would be the same.

Personally I prefer the boolean. But that's only my preference; in most
cases there's no technical reason to choose one over the other.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 11 '06 #15
Rik wrote:
>$row['isarchived'] is already a 0/1 value (Tinyint(1)), so really the
(bool) cast is a no-op. My bad. You could just use $isArchived =
$row['isarchived']

What do you mean by no-op? I'm no native speaker, please explain. And
already 0/1? 0/1 is an integer, not a boolean.
Due to the loose type association false (boolean) == 0 (integer).
Similarly, true (boolean) == (not) 0 (integer)

So, given that a Tinyint(1) can only return 0 and 1 this has a direct
1:1 mapping through the implied type casting of PHP to false and true.

That's what I meant by no-op (PHP will handle it for you)

-david-

Jul 11 '06 #16
Jerry Stuckle wrote:
No-Op is from Assembler - "No Operation", although it is more correctly
referred to as "noop". It's an instruction which does nothing but take
time and memory. Useful when you need to delay a couple of CPU cycles
for something else to occur.

And you are correct - the (bool) is needed if you want the value to be a
boolean. However, if you're only going to test it, you could leave it
as an integer 1/0 instead of true/false; the result would be the same.

Personally I prefer the boolean. But that's only my preference; in most
cases there's no technical reason to choose one over the other.
Wow! Now I really feel like a programming fossil. LOL!
Guess all those days of assembler programming are catching up on me. ;-)

RE: using (bool)
I agree. Anything that makes the type/intent clearer should be
encouraged.

-david-

Jul 11 '06 #17
Why are you complicating your life. Just do the following

print (int) false; // prints 0
print (int) true; // prints 1
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?
Jul 11 '06 #18
Rik
David Haynes wrote:
Rik wrote:
>>$row['isarchived'] is already a 0/1 value (Tinyint(1)), so really
the (bool) cast is a no-op. My bad. You could just use $isArchived =
$row['isarchived']

What do you mean by no-op? I'm no native speaker, please explain.
And already 0/1? 0/1 is an integer, not a boolean.

Due to the loose type association false (boolean) == 0 (integer).
Similarly, true (boolean) == (not) 0 (integer)

So, given that a Tinyint(1) can only return 0 and 1 this has a direct
1:1 mapping through the implied type casting of PHP to false and true.

That's what I meant by no-op (PHP will handle it for you)
It does in most cases, however, if one states:"I would translate the 0/1
from MySQL into true/false at the lowest level I could.", one provides an
answer :-).

Grtz,
--
Rik Wasmus
Jul 11 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: harry | last post by:
I have an object that's passed in to a function as a parameter i.e public boolean getProjectTitle(ProjectHeader_DTO obj) {...} If I then call a method on this object inside the function i.e...
20
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> ...
1
by: Eric Ellsworth | last post by:
Hi, I'm having trouble passing an array to a function, like this: Dim sFieldsNotRequired(11), sFieldsWithSpecificValues(5) As String Dim sCaseErrMsgs As String Dim vFieldsSpecificValues(5) As...
2
by: Manish | last post by:
Hi, I m facing a problem in re-writing a code from VB to C #. We are using a third party DLL to develop a application of Fax. The current application is already written in VB and we have to...
7
by: DareDevil | last post by:
I have written a method that should modify the folder path passed to it into one that exists and is selected by the user. It then returns a boolean depending on whether a folder path was selected by...
5
by: James Wong | last post by:
Dear all, I've a web service function and it contains a parameter in System.Text.Encoding. I found that the data type of this parameter in caller application becomes MyWebSvcName.Encoding...
2
by: Jacques Wentworth | last post by:
Hi I'm trying to pass a collection from a .NET DLL to a VB6 app. I get a error 13 (type mismatch) when I do so. I passed back a boolean variable without any problems, but when I try the...
10
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var...
4
by: John Sheppard | last post by:
Hello there I was wondering if anyone could help me, I am trying to pass a typed dataset to a dialoged child form by reference. I have binding sources sitting on the child form. So to refresh...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.