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($te stBool) {
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? 18 6051
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($te stBool) {
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?
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($te stBool) {
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($te stBool) {
printf("Test constructed with testBool = %s\n", $testBool ? 'true' :
'false');
}
}
$testing = new Test(true);
$testing = new Test(false);
?>
-david-
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).
<di******@hotma il.comwrote in message
news:11******** **************@ m79g2000cwm.goo glegroups.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***@bhg byrzcv.arg)
Kimmo Laine wrote:
<di******@hotma il.comwrote in message
news:11******** **************@ m79g2000cwm.goo glegroups.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***@bhg byrzcv.arg)
One thing I have noticed is that Diet Dr. Pepper really doesn't taste
like regular Dr. Pepper.
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($te stBool) {
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()" ;
?>
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($s ql);
while( $row = mysql_fetch_ass oc($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($te stBool) {
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($te stBool) {
$this->testBool = $testBool;
}
// accessor
public function getTestBool() {
return $this->testBool;
}
// mutator
public function setTestBool($te stBool) {
$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("getTest Bool returned true\n");
}
// mutator
$my_test->setTestBool(fa lse);
// toString
printf('testBoo l 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 setTestBoolFrom MySQL() if you want.
-david-
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.
Kimmo Laine wrote:
<di******@hotma il.comwrote in message
news:11******** **************@ m79g2000cwm.goo glegroups.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***@bhg byrzcv.arg)
Ohhh, burned.
I'll be here all night, folks. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
obj.setTitle("test") then using obj.getTitle() from outside the function it
displays the property set correctly!
But doing something like this inside the function doesn't -
ProjectHeader_DTO x = new ProjectHeader_DTO();
|
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>
bool1=True
lst1=
def func1(arg1): arg1.append(4)
|
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 Variant
sFieldsNotRequired(0) = "middleName"
|
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 convert it into
C#. In VB we are using a function from the DLL whose signature is like
:
Declare Function RFVB_SendFiles1 Lib "RF2VB.DLL" (ByVal hServer As
Long, _
ByVal lFI10Object As
|
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 the user
It then dawned on me that I was passing in a readonly property into the method yet neither at compile time or runtime was I getting any kind of error or warning.
I tested with a simple string field and it alter the string as expected but...
| |
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 (where "MyWebSvcName" is
the name of my web service). I don't know how to assign value to this
parameter. (I'm using VB.NET 2003.)
Here are some sample codes:
|
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 collection I get the error.
As you can see the function does not do anything at the moment, so it
must be some issue passing the collection.
Thanks
|
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 boldLink=document.getElementById('cmtbold');
|
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 them I just
set their datasource. I am guessing this is probably what is causing the
problem. Is there a better way to do this?
Anyway this all works happily and things show up when the record already
exists but I have 2 problems ;
1) When I add...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |