|
Posted by David Haynes on 12/17/00 11:52
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-
Navigation:
[Reply to this message]
|