multiple constructors-overloading ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rizjav_86
Forum Newbie
Posts: 16
Joined: Wed Feb 24, 2010 10:09 am

multiple constructors-overloading ?

Post by rizjav_86 »

Short question, does PHP support function overloading, if yes, please tell me how ?

Explanation:
Ok so in Java you can have multiple functions with same names but different parameters, and the JVM is smart enough to figure out which one you are calling. e.g.

Code: Select all

// Java code..
class Person {
     private String name;
     private int age;
 
     public Person(String name) {
        this.name = name;
     }
 
     public Person(String name, int age) {
        this.name = name;
        this.age = age;       
     }
}
// and now I can call them like
Person person = new Person("Rizjav");
Person person = new Person("rizjav", 23);
Is this is possible in PHP ? I have tried it but it doesn't work, any thoughts ?

I know I can pass a flag variable in the constructor and then figure out what function I need to call by checking flag's value e.g. if flag==1 then do this else do that...but I think that's not how a real OOP language should work.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: multiple constructors-overloading ?

Post by Darhazer »

Hi,
Unfortunately PHP does not support method overloading.
You can...
define default values for the arguments
... or use different factory methods, if the initialization should be different.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: multiple constructors-overloading ?

Post by Weirdan »

It's fairly common in php to set default value for a parameter to some invalid value and then analyze it in the method/constructor:

Code: Select all

 
class Person {
  private $_name = '';
  private $_age = 0;
  public function __construct($name, $age = -1) {
    $this->_name = $name;
    if ($age > 0) {
      $this->_age = $age;
    }
  }
}
 
$person = new Person("Rizjav");
$anotherPerson = new Person("Rizhav", 23);
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: multiple constructors-overloading ?

Post by requinix »

You're right: a real OOP language should have overloading. Unfortunately, PHP is not a real OOP language. Not yet.

Alternatives:
1. When it's a matter of supporting more arguments than another, use optional arguments (a la Weirdan)
2. When appropriate, use factories (a la Darhazer)
3. Use similar but differently named methods
4. Hide those methods, and use __call to simulate overloading of one method by testing argument types
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: multiple constructors-overloading ?

Post by Eran »

Unfortunately, PHP is not a real OOP language
I don't want to start a flame-war here, but having this or that feature does not decide if a language is OO or not. PHP has had OO support since PHP4 and it has gotten much better in PHP5. And as you detailed in your post, there are ways to achieve what the OP wanted (I'll add another - func_get_args() ).

It's probably best not to try and apply methodologies from other languages blindly in PHP though, write code that works to the language strengths and features.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: multiple constructors-overloading ?

Post by requinix »

pytrin wrote:
Unfortunately, PHP is not a real OOP language
I don't want to start a flame-war here, but having this or that feature does not decide if a language is OO or not. PHP has had OO support since PHP4 and it has gotten much better in PHP5. And as you detailed in your post, there are ways to achieve what the OP wanted (I'll add another - func_get_args() ).
My truck can do 0-60 in a few seconds and can run at 150 mph comfortably, but that doesn't make it a sports car.

PHP has support for a few nice things in OOD, yes, but as you said just having the features doesn't mean much. PHP 5.3 is still a procedural language encouraging procedural design.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: multiple constructors-overloading ?

Post by Eran »

My truck can do 0-60 in a few seconds and can run at 150 mph comfortably, but that doesn't make it a sports car.
We can throw analogies all day, but it means nothing and has nothing to do with the issue. I could say that procedural is a horse and OOP is a car (which is a more apt description of the difference in my opinion), then the truck (PHP) is definitely a car even though it's not a sport car like {enter your favorite OO language here}. At least it can carry some heavy cargo even if it can't go as fast or has a convertible roof. PHP can be used as an OO language as well as a procedural one. Personally, I can't recall the last time I used procedural programming with it.

You might argue what constitutes an OO language on a feature-by-feature basis, but that would be just your personal preference / take on it. The fact is that PHP allows you to develop with OO methodologies and patterns as the core. I would claim that is what defines an OOP language more than any particular feature.
rizjav_86
Forum Newbie
Posts: 16
Joined: Wed Feb 24, 2010 10:09 am

Re: multiple constructors-overloading ?

Post by rizjav_86 »

Thanks guys for all the thoughtful replies.

@pytrin:

PHP is not Object Oriented. Because in PHP stuff is not oriented on objects.
In PHP you can write Classes but you don't have to write Classes.

In java even if you have to print your name, you need to create a Class and then at least a main "static" method.

One more reason is that PHP uses Interpreter, and as far as I believe a true OO Language first needs to Compile the entire code to see that every function is defined in some Class. (but that's just my common sense not a research :D )
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: multiple constructors-overloading ?

Post by Darhazer »

In C++ you don't have to write classes, but it's still object oriented
So, enforcing writing classes is not the one that makes a language object oriented, not writing them makes your programming object oriented. But this is a long topic and it's far away from this topic on overloading and probably is one we can never achieve agreement on :) Because the beauty of the world, as well as the beauty of the programming is the variety :)
Post Reply