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 9 14573
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
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
Berislav Lopac wrote: Cameron wrote:
Stijn Goris wrote:
hi all,
Is it possible to declare 2 constructors in the same class? The constructo rs 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
"Stijn Goris" <me*****@hotmai l.com> wrote in message
news:q5******** ***********@pho bos.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_metho ds($objApplicat ionObject);
foreach ($arrMethods as $strCurrentMeth odName)
{
if (strpos($strCur rentMethodName, "autorun"))
{
$strToExecute = '$void =
$objApplication Object->'.$strCurrentM ethodName.'();' ;
eval($strToExec ute);
}
}
}
}
With all that in place, in the main code I do this:
$objMyNewObject = Factory::Create Object();
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.
> class Factory { function CreateObject() { $objApplication Object = new B();
just to make it work out of the box - I took this from some existing code -
you get the idea.
MS
"Stijn Goris" <me*****@hotmai l.com> wrote in message news:<q5******* ************@ph obos.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($par 1, $par2, $opPar1 = "", $opPar2 = "")
{
echo $par1 ." ". $par2 . " ";
if($opPar1 != ""){
echo $opPar1 . " ";
}
if($opPar2 != ""){
echo $opPar2;
}
echo "<br/>";
}
?>
hope this might help.
"Cameron" <fo*@bar.invali d> wrote in message
news:bv******** **@newsg1.svr.p ol.co.uk... Berislav Lopac wrote: Cameron wrote:
Stijn Goris wrote:
hi all,
Is it possible to declare 2 constructors in the same class? The constructo rs 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
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_obje ct($para1)
{
$this ->par1 = $para1;
}
//2parameter
function initialize_obje ct($para1,$para 2)
{
$this ->par1 = $para1;
$this ->par2 =$para2;
}
}
//make object
$myclass1 = new myclass();
//initialize with data
$myclass1->initialize_obj ect(5);
//make other object
$myclass2 = new myclass();
$myclass2 ->initialize_obj ect(6,"genesis" );
"Farhan" <go**********@h otmail.com> schreef in bericht
news:b6******** *************** ***@posting.goo gle.com... "Stijn Goris" <me*****@hotmai l.com> wrote in message
news:<q5******* ************@ph obos.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($par 1, $par2, $opPar1 = "", $opPar2 = "") { echo $par1 ." ". $par2 . " "; if($opPar1 != ""){ echo $opPar1 . " "; } if($opPar2 != ""){ echo $opPar2; } echo "<br/>"; } ?>
hope this might help.
Farhan wrote: "Stijn Goris" <me*****@hotmai l.com> wrote in message news:<q5******* ************@ph obos.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($par 1, $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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 an idea that we can have private constructors and destructors
but am not able to find a situation where they can be used...
Regards
RVG
rajeshgarg@opussoft.com
|
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 kind. It sounds simple
but evidently .NET has difficulty with this concept for some reason. I do
understand that .NET objects are created on the GC heap but that doesn't
mean that they couldn't be copied from another object of the same kind when...
|
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 argument.....
thanks folks
steve
|
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 that they are not mutually exclusive of one another.
The above classification assimilates my knowledge of having used
constructors in both the above manners.
|
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 work right,
although I quite don't see what is does in addition to the 2nd constructor.
Also, the example works fine without message calls in either constructor
(the numerical answer is still there and correct!).
I exected it to no longer work...
| |
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 permutations
for constructors (none, default, two argument) and method call in body of
constructor (none and one).
Maybe this example is not representative, but for this example I found the
following:
1. Without any constructors, the program works fine...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |