jump to navigation

Multiple Constructor in PHP January 31, 2008

Posted by ordinarywebguy in PHP, PHP 5, Programming.
5 comments

How?

See code:

class MultipleConstructor {

private $info = ”;

function __construct() {
$argv = func_get_args();
switch( func_num_args() )
{
default:
case 1:
self::__construct1($argv[0]);
break;
case 2:
self::__construct2( $argv[0], $argv[1] );
break;
}
}

function __construct1($value) {
$this->info = $value;
}

function __construct2($value, $value2) {
$this->info = $value . ” ” . $value2;
}

function get() {
return $this->info;
}
}

$a = new MultipleConstructor(‘Value 1′);
echo $a->get();

$b = new MultipleConstructor(‘Value 1′, ‘Value 2′);
echo $b->get();

Viola!!!

It’s Not a Bug :-p August 3, 2007

Posted by ordinarywebguy in PHP, Programming.
4 comments

Just a follow up that I indeed deferred to my post: in_array PHP Function Bug.

I posted the said blog post in PHPUGPH and lead me to fully understand that it was not a bug. As Cruizer said:

if it’s not a bug, then it’s a gotcha. i don’t think that would be obvious to any programmer. Tongue bad design, i’d say.

I definitely agree with that statement. :D

To clear up everything that it was not a bug. See code:

$str = “this is a string”;

if ($str == 0) {
print “true”;
} else {
print “false”;
} # end if

Output: true

Wonder why the output is “true”?
See here: http://www.php.net/manual/en/types.comparisons.php

In addition, RJ said:

As I understand the checking by PHP for the loose comparison of a string and an integer, the PHP engine will call the intval() function for the string before comparing it with the integer. Therefore “any string” == 0 will actually be intval(“any string”) == 0 which will result to 0 == 0. It means that it is not a boolean comparison.

Again, that’s not a bug, gotcha, or whatever you call it but instead a bad design. :(

in_array PHP Function Bug July 27, 2007

Posted by ordinarywebguy in PHP, Programming.
6 comments

See the code:

<?php
$array1 = array(‘php’, ‘java’, ‘ruby’, ‘asp’);
$array2 = array(‘php’ => ‘PHP.net’, ‘java’ => ‘java.com’, 0 => ‘javascript’, 1 => ‘actionscript’);

foreach ($array2 as $key => $value) {
if ( in_array($key, $array1) ) {
print “in array: $key <br />”;
} else {
print “not in array: $key <br />”;
} # end if

} # end foreach

?>

Output:
in array: php
in array: java
in array: 0
not in array: 1

Expected Output:
in array: php
in array: java
not in array: 0
not in array: 1

Though the expected output above can be achieve via supplying TRUE to its third paramater which will set strict typing comparison between the value and array:

<?php
$array1 = array(‘php’, ‘java’, ‘ruby’, ‘asp’);
$array2 = array(‘php’ => ‘PHP.net’, ‘java’ => ‘java.com’, 0 => ‘javascript’, 1 => ‘actionscript’);

foreach ($array2 as $key => $value) {
if ( in_array($key, $array1, TRUE) ) {
print “in array: $key <br />”;
} else {
print “not in array: $key <br />”;
} # end if

} # end foreach

?>

How come that there’s a “0″ value on array $array1? That’s why I consider it a bug. I don’t know if someone has already experience this. I hope somebody could further explain this to me. I’ll also post this bug at PHP.net.

Note:
The script runs in PHP Version: 5.1.1

Web Based Command Line April 17, 2007

Posted by ordinarywebguy in Cool Websites, Programming, Ruby.
3 comments

Yes, you read it correctly “Web Based Command Line“.

I’ve just discovered it trying to have my first bite with Ruby Programming Language. I found it very tasty after biting it because of its hands on tutorial. This is what I’m talking about “Web Based Command Line”. Check this out here: http://tryruby.hobix.com/. It uses the Javascript Framework prototype.js and moo.fx library. The site is extremely cool. I bet this will be one of the factors to inspire fellow developers to bite more and get their hands dirty on Ruby.

Links:
http://www.ruby-lang.org/en/
http://tryruby.hobix.com/

Other Web Based Command Line:
http://a-i-studio.com/cmd/

I am a Ruby Language!?!! March 14, 2007

Posted by ordinarywebguy in Cool Websites, Programming.
add a comment

Surprisingly this was the result of the quiz “Which Programming Language Are You?”.

Try it also here: http://www.bbspot.com/News/2006/08/language_quiz.php