473,394 Members | 1,528 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,394 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 14556
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.