473,394 Members | 1,752 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.

PHP constructors

111 100+
Hi

i'm new to Object Oriented Programming in PHP5, but have done this before in Java and C++.

I not sure if this is possible in PHP5 but what i'm trying to do is create two constructors in my class. One a default constructor and another a parameterised constuctor. (Firstly, is it possible to have 2 constructors in a class)

I want the parameterised constructor to call the default constructor, but i can't find any syntax that allows this to be done.

In the parameterised constructor, I am trying to call the default constructor by using:

[PHP]$this->__construct();[/PHP]

But that does not seem to work;


Also, at the point that I have declared the second constructor I get the error:

Fatal error: Cannot redeclare class_name::__construct()
which suggest that i cannot create another constructor.

But, if two constructors can't be created in a class, how does one go about implementing a default and parameterised constructor for a class.
Jul 10 '08 #1
1 2633
pbmods
5,821 Expert 4TB
Heya, Nitin.

You can use default values for your parameters:
Expand|Select|Wrap|Line Numbers
  1. public function __construct( $name = null, $grade = null )
  2. {
  3.   if( $name ) $this->name = $name;
  4.   if( $grade ) $this->grade = $grade;
  5. }
  6.  
Calling the constructor without specifying any parameters will cause PHP to use the default values (in this case, null). Have your constructor check for nulls to decide whether to use the parameters.

Or you can simply set the default values as you need; they don't have to be null.
Jul 11 '08 #2

Sign in to post your reply or Sign up for a free account.

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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...

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.