469,286 Members | 2,476 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

2 constructors in one class

hi all,

Is it possible to declare 2 constructors in the same class? The constructors
would have different number of parameters parameters...

kind regards
Stijn
Jul 17 '05 #1
9 14371
Stijn Goris wrote:
hi all,

Is it possible to declare 2 constructors in the same class? The constructors
would have different number of parameters parameters...

kind regards
Stijn


in other words does PHP support overloading?

Nope

~Cameron
Jul 17 '05 #2
Cameron wrote:
Stijn Goris wrote:
hi all,

Is it possible to declare 2 constructors in the same class? The
constructors would have different number of parameters parameters...

kind regards
Stijn


in other words does PHP support overloading?


Sure it does, only not as Java programmers would expect. Using optional
parameters is basically overloading, although you are stuck with only one
function.

Therefore, the OP doesn't need two constructors -- one, with optional
parameters and some conditional code to handle the differences, should be
enough.

Berislav
Jul 17 '05 #3
Berislav Lopac wrote:
Cameron wrote:
Stijn Goris wrote:
hi all,

Is it possible to declare 2 constructors in the same class? The
constructors would have different number of parameters parameters...

kind regards
Stijn


in other words does PHP support overloading?

Sure it does, only not as Java programmers would expect. Using optional
parameters is basically overloading, although you are stuck with only one
function.

Therefore, the OP doesn't need two constructors -- one, with optional
parameters and some conditional code to handle the differences, should be
enough.

Berislav


I aren't a Java programmer and also overloading in other languages is
done with multiple functions of the same names and different parameters,
but there are restrictions on what you can do with optional parameters
that is if a parameter is optional then all following ones must be also,
at least I believe that applies to PHP as it does to other languages.

~Cameron



Jul 17 '05 #4
"Stijn Goris" <me*****@hotmail.com> wrote in message
news:q5*******************@phobos.telenet-ops.be...
hi all,

Is it possible to declare 2 constructors in the same class? The constructors would have different number of parameters parameters...

kind regards
Stijn

This *probably* isn't what you're asking for but you *might* find it useful.

I have a requirement for chained constructors - when an object is
instantiated, I want a constructor-style function to run from each class
from which the final class inherits. PHP4 doesn't do that AFAIK. PHP5
might, I haven't looked. But anyway here's how I do it (no idea how this
source is going to look when posted, I expect it will be scrambled in
respect of indenting etc. - but should be clear enough hopefully).

class A
{
function A_autorun()
{
echo "Hello";
}
}

class B extends A
{
function B_autorun()
{
echo "Goodbye";
}
}
class Factory
{
function CreateObject()
{
$objNewObject = new B();

$arrMethods = get_class_methods($objApplicationObject);

foreach ($arrMethods as $strCurrentMethodName)
{
if (strpos($strCurrentMethodName,"autorun"))
{
$strToExecute = '$void =
$objApplicationObject->'.$strCurrentMethodName.'();';
eval($strToExecute);
}

}

}

}

With all that in place, in the main code I do this:

$objMyNewObject = Factory::CreateObject();

and receive this output:

HelloGoodbye

..

Hope this is useful to someone, perhaps even the OP. Obviously there may
well be better ways of doing this, and I guess it would make sense to try
and revise one's design so that this kind of thing wasn't necessary...

Cheers,

Matt Saunders.
Jul 17 '05 #5
> class Factory
{
function CreateObject()
{
$objApplicationObject = new B();


just to make it work out of the box - I took this from some existing code -
you get the idea.

MS
Jul 17 '05 #6
"Stijn Goris" <me*****@hotmail.com> wrote in message news:<q5*******************@phobos.telenet-ops.be>...
hi all,

Is it possible to declare 2 constructors in the same class? The constructors
would have different number of parameters parameters...

kind regards
Stijn


<?php
myFunction(1, 2);
myFunction(1, 2, 3);
myFunction(1, 2, 3, 4);

function myFunction($par1, $par2, $opPar1 = "", $opPar2 = "")
{
echo $par1 ." ". $par2 . " ";
if($opPar1 != ""){
echo $opPar1 . " ";
}
if($opPar2 != ""){
echo $opPar2;
}
echo "<br/>";
}
?>

hope this might help.
Jul 17 '05 #7

"Cameron" <fo*@bar.invalid> wrote in message
news:bv**********@newsg1.svr.pol.co.uk...
Berislav Lopac wrote:
Cameron wrote:
Stijn Goris wrote:

hi all,

Is it possible to declare 2 constructors in the same class? The
constructors would have different number of parameters parameters...

kind regards
Stijn

in other words does PHP support overloading?

Sure it does, only not as Java programmers would expect. Using optional
parameters is basically overloading, although you are stuck with only one function.

Therefore, the OP doesn't need two constructors -- one, with optional
parameters and some conditional code to handle the differences, should be enough.

Berislav


I aren't a Java programmer and also overloading in other languages is
done with multiple functions of the same names and different parameters,
but there are restrictions on what you can do with optional parameters
that is if a parameter is optional then all following ones must be also,
at least I believe that applies to PHP as it does to other languages.

~Cameron


Since I was a JAVA programmer I am somewhat dissapointed that PHP doesn't
support overloading. I know I can get around this but I don't like to use
those tricks. I hope will have better support.

thanks for the answers btw

regrads
Stijn
Jul 17 '05 #8
Isn't possible to make a empty constructor fo simply construct the object.
And then some class methods to put data in the member data?

e.g

class myclass
{

var $par1;
var$par2;
var$par3;

function myclass(){}; //empty constructor

//1 parameter
function initialize_object($para1)
{
$this ->par1 = $para1;
}

//2parameter

function initialize_object($para1,$para2)
{
$this ->par1 = $para1;
$this ->par2 =$para2;
}

}

//make object

$myclass1 = new myclass();

//initialize with data

$myclass1->initialize_object(5);

//make other object

$myclass2 = new myclass();

$myclass2 ->initialize_object(6,"genesis");

"Farhan" <go**********@hotmail.com> schreef in bericht
news:b6**************************@posting.google.c om...
"Stijn Goris" <me*****@hotmail.com> wrote in message

news:<q5*******************@phobos.telenet-ops.be>...
hi all,

Is it possible to declare 2 constructors in the same class? The constructors would have different number of parameters parameters...

kind regards
Stijn


<?php
myFunction(1, 2);
myFunction(1, 2, 3);
myFunction(1, 2, 3, 4);

function myFunction($par1, $par2, $opPar1 = "", $opPar2 = "")
{
echo $par1 ." ". $par2 . " ";
if($opPar1 != ""){
echo $opPar1 . " ";
}
if($opPar2 != ""){
echo $opPar2;
}
echo "<br/>";
}
?>

hope this might help.

Jul 17 '05 #9
Farhan wrote:
"Stijn Goris" <me*****@hotmail.com> wrote in message news:<q5*******************@phobos.telenet-ops.be>...
hi all,

Is it possible to declare 2 constructors in the same class? The constructors
would have different number of parameters parameters...

kind regards
Stijn

<?php
myFunction(1, 2);
myFunction(1, 2, 3);
myFunction(1, 2, 3, 4);

function myFunction($par1, $par2, $opPar1 = "", $opPar2 = "")
{
echo $par1 ." ". $par2 . " ";
if($opPar1 != ""){
echo $opPar1 . " ";
}
if($opPar2 != ""){
echo $opPar2;
}
echo "<br/>";
}
?>

hope this might help.


You should take a look at the Function Handling functions, specifically
func_get_args()
--> http://php.net/manual/en/function.func-get-args.php
--> http://php.net/manual/en/ref.funchand.php

--
Chris Jenkinson
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Rajesh Garg | last post: by
42 posts views Thread by Edward Diener | last post: by
6 posts views Thread by Stephen Martinelli | last post: by
4 posts views Thread by Sathyaish | last post: by
1 post views Thread by CARIGAR | 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.