Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Multiple Constructor in PHP

  http://ordinarywebgu        2011-10-01 02:28:22       9,237        0    

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!!!

Source : http://ordinarywebguy.wordpress.com/2008/01/31/multiple-constructor-in-php/

PHP  CONSTRUCTOR  MULTIPLE CONSTRUCTOR 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  0 COMMENT


No comment for this article.