Solved

Constructor overloading?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 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…

910 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

22 Experts available now in Live!

Get 1:1 Help Now