473,324 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 14554
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
6
by: Stephen Martinelli | last post by:
thanks for the help...just one more question.... can a class have more then two parameterized constructors?..i would like to be able to instanciate the class with a different number of...
4
by: Sathyaish | last post by:
What is a private constructor, and why would a class have one? What are the other kinds of constructors besides: (1) public constructors; and (2) parameterized constructors And I understand...
10
by: John | last post by:
Trying to find out what is essential / optional, I made an extremely simple Class and Module combination to add two numbers. (see below) It appears that an empty constructor is needed n order to...
3
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.