Solved

Constructor overloading?

Posted on 2003-11-28
4
385 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
Comment Utility
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
Comment Utility
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
Comment Utility
Holy

looks like i read this question to fast.
0
 

Author Comment

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

Greetings, -ulf-
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how to resize preview image 4 27
html input clean up 3 27
session dropped in IE 10 18
php variable basic question 12 27
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 …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 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

6 Experts available now in Live!

Get 1:1 Help Now