Connect with Expertise | Find Experts, Get Answers, Share Insights

constructor overloading in php

emrahayanoglu@gmail.com
 
Posts: n/a
#1: Jun 4 '06
Hello Everyone,

I looked everywhere for that problem. Does anyone know that how can i
overload constructor function in php 5?

Best Regards,

Emrah Ayanoglu


Sjoerd
 
Posts: n/a
#2: Jun 4 '06

re: constructor overloading in php



emrahayanoglu@gmail.com wrote:[color=blue]
> Does anyone know that how can i
> overload constructor function in php 5?[/color]

Yes. PHP4 or PHP5? What have you tried already?

emrahayanoglu@gmail.com
 
Posts: n/a
#3: Jun 4 '06

re: constructor overloading in php


i tried like other languages. This is an example.
PHP5

class emr
{
function __construct($username)
{
}
function __construct($username, $password)
{
}
function __construct($username, $password, $name)
{
}
}
Then i have that error:Fatal error: Cannot redeclare emr::__construct()

Janwillem Borleffs
 
Posts: n/a
#4: Jun 4 '06

re: constructor overloading in php


emrahayanoglu@gmail.com wrote:

Don't multipost
[color=blue]
> i tried like other languages. This is an example.
> PHP5
>[/color]

See my reply in alt.comp.lang.php; in short: Overloading like in Java is not
supported in PHP.


JW


Rik
 
Posts: n/a
#5: Jun 5 '06

re: constructor overloading in php


emrahayanoglu@gmail.com wrote:[color=blue]
> i tried like other languages. This is an example.
> PHP5
>
> class emr
> {
> function __construct($username)
> {
> }
> function __construct($username, $password)
> {
> }
> function __construct($username, $password, $name)
> {
> }
> }[/color]

If that's what you wnat, why not declare some defaults for the variables:
function __construct($username, $password=false, $name=false)
{
if($name!==false)//execute some code
if($password!==false)//domething else
}
Etc.
Which makes you able to:
$a = new emr('username');

And all arguments left out will result in their default values.

Else, it's up to the earlier mentioned func_get_args();

Grtz,
--
Rik Wasmus


Closed Thread