469,266 Members | 1,679 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,266 developers. It's quick & easy.

constructor overloading in php

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

Jun 4 '06 #1
4 31899

em***********@gmail.com wrote:
Does anyone know that how can i
overload constructor function in php 5?


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

Jun 4 '06 #2
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()

Jun 4 '06 #3
em***********@gmail.com wrote:

Don't multipost
i tried like other languages. This is an example.
PHP5


See my reply in alt.comp.lang.php; in short: Overloading like in Java is not
supported in PHP.
JW
Jun 4 '06 #4
Rik
em***********@gmail.com wrote:
i tried like other languages. This is an example.
PHP5

class emr
{
function __construct($username)
{
}
function __construct($username, $password)
{
}
function __construct($username, $password, $name)
{
}
}


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
Jun 5 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Sergey Krushinsky | last post: by
8 posts views Thread by Sam Sungshik Kong | last post: by
31 posts views Thread by Peter E. Granger | last post: by
14 posts views Thread by Klaus Löffelmann | last post: by
7 posts views Thread by Doug | last post: by
7 posts views Thread by coinjo | last post: by
12 posts views Thread by Rahul | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.