Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Constructor overloading?

Posted on 2003-11-28
4
Medium Priority
?
416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

715 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