473,406 Members | 2,713 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,406 software developers and data experts.

constructors

Hi,

Is it possible to have more constructors in the same class?

e.g

class person
{
function person($name)
{
}

function search($name,$age)
{
}

}
//make object

$person1 = new person("genesis");
$person2 = new person("U2",40);

thx.

Alain
Jul 17 '05 #1
3 2250
oeps,
That's wrong it must be:

class person
{
function person($name)
{
}

function person($name,$age)
{
}

}
//make object

$person1 = new person("genesis");
$person2 = new person("U2",40);

thx.
"alain dhaene" <a.******@instruct.be> schreef in bericht
news:40**********************@news.skynet.be... Hi,

Is it possible to have more constructors in the same class?

e.g

class person
{
function person($name)
{
}

function search($name,$age)
{
}

}
//make object

$person1 = new person("genesis");
$person2 = new person("U2",40);

thx.

Alain

Jul 17 '05 #2
alain dhaene wrote:
Is it possible to have more constructors in the same class?


As I understand it, all methods in PHP follow the general functions rule,
which is that you can't redeclare a function. Unlike Java, where a method's
signature includes the arguments and the return type besides the name
itself, in PHP the "signature" is only the method's name.

However, you can "overload" a function by stating optional arguments, which
are defined in fuction definition, but can be avoided when calling the
function/method.

In your example, the constructor could be:

person($name, $age = NULL)

and your two example calls would work.

Berislav
Jul 17 '05 #3
Berislav Lopac wrote:
In your example, the constructor could be:

person($name, $age = NULL)

and your two example calls would work.


If you need different kinds of arguments for different constructors
(e.g. person($name) and person($sex, $age)), you can also do the following:

class person {
function person(){
switch (func_num_args()) {
case 1:
$this->name = func_get_arg(0);
break;
case 2:
$this->sex = func_get_arg(0);
$this->age = func_get_arg(1);
break;
default:
die ('wrong number of arguments for constructor');
}
}
}

You could further check the types of the arguments in different places,
e.g. if func_get_arg(0) is a string of "m" or "f", treat the call as
person($sex, $age), otherwise treat it as person($name, $age)

Jochen
Jul 17 '05 #4

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...
22
by: Peter Morris [Droopy eyes software] | last post by:
Look at these two classes public class Test { public readonly string Name; public Test(string name)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
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,...
0
Oralloy
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,...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.