471,893 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,893 software developers and data experts.

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 32054

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 zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.