Solved

Constructor overloading?

Posted on 2003-11-28
4
407 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

739 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