Solved

Constructor overloading?

Posted on 2003-11-28
4
401 Views
Last Modified: 2008-02-01
Hi out there,
I simply want to have a class that has multiple contructors. But this wont work.
In this example the constructor with the one argument is always called, even if i do new DocID();
Where am I wrong?
How can i do this?
Thanks in advance!
Greetings, -ulf-
Example
class DocID
{
      function DocID()
      {
            print "empty";
      }
      
      function DocID($repository,$case,$doc,$page)
      {
            print "3 args";
      }
      
      function DocID($httpGetVars)
      {
            print "1 arg";
      }
}
0
Comment
Question by:ulf_k
  • 2
4 Comments
 

Expert Comment

by:mixmastr
ID: 9837929
class yeah {

 function test() {
 echo "test";
 }

 function bla() {
 echo "bla";
 }
}

$my = new yeah;
$my->bla();

Think this should work.
0
 
LVL 13

Accepted Solution

by:
lozloz earned 75 total points
ID: 9838063
there's not constructor there mixmastr?

i think you're thinking of java/c++, php can't have multiple constructors as far as i'm aware, you're going to have to combine it all into one function with default arguments and go from there i suppose..

class DocID
{
     function DocID($repository = "",$case = "",$doc = "",$page = "")
     {
          if(!empty($repository) && !empty($case) && !empty($doc) && !empty($page)) {
            \\ print "3 args";
          } elseif(!empty($repository)) {
            \\ print "1 arg";
          } else {
            \\ empty
          }
     }
}

$doc = new DocID(args..);

loz
0
 

Expert Comment

by:mixmastr
ID: 9838373
Holy

looks like i read this question to fast.
0
 

Author Comment

by:ulf_k
ID: 9840213
Thanx for your suggestion,
i did it that way.
Sad but true that overloading is not possible.

Greetings, -ulf-
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now