Hi, kind of new in PHP since I'm reading a 'novice to pro' one :)
I was wondering how php experienced users deal with these two (at least for me) hot topics? What's your way of implementing it? Is there any 'convention' about this and not a merely a personal 'hack'?

I understand that due the nature of php being a loosely typed language neither method nor operator overloading will ever be implemented (even in version 6 from what i've read) since as in other languages mehod overloading works with number and type of parameter as well as its order, and as for operator overloading if we talk about 'real' oop and pretend to be 'picky'...the + operator may be considered no a oop feature, it perhaps should be a method add() or something... but that's another history or pehaps the same?


7 Years
Discussion Span
Last Post by ShawnCplus

You are correct, method overloading is and will never be in the language. However, there are ways to get around this. The func_get_args function can be used to retrieve an array of all arguments passed to the function. Also, as you mentioned, PHP is loosely typed but can be "Type Hinted."

One way to go about faking method/function overloading is to use is_<type> functions on the passed arguments to swap them around. ie.,

function swapParams($someint, $somestring)
  if (is_int($somestring) && is_string($someint)) {
   $tmp = $someint;
   $someint = $somestring;
   $somestring = $tmp;
  // do stuff here

Also correct is that there is no operator overloading. Once again though it can be faked. The Magic Methods can be used to a very limited extent to overload the equivalent [] , -> and :: operators.

Edited by mike_2000_17: Fixed formatting


I 've strugled what you've said a little and those 'magic functions' can be part of a workaround

PHP is loosely typed but can be "Type Hinted."

However, type hinting only works with objects as far as I've read and although for 'primitive' types (int, float, string, etc) something like:

class Bool
     private $theBool;
     public static function typeHint($val)
          return is_set($value);

can be done, i have the felling that this will not do the trick in the long run...especially as regards the op overloading...

What you think about?


like I said, for primitives you can use the is_<type> methods (is_int, is_string, is_bool, etc). Keep in mind that if you're using this for OOP and you have a child class with a method of the same name taking a different number of arguments the function or your design is probably wrong. Also be sure that you're not confusing function overloading and polymorphism. They aren't the same thing. Polymorphism is implicit in PHP due to being loosely typed.

class Shape
  protected $name;
  public function setName($name) { $this->name = $name; }
  public function getName() { return $this->name; }

Class Circle extends Shape {}

function getshapename(Shape $someshape)
 // yeah, dumb function, just an example
  return $someshape->getName();

$shape = new Shape();
$circle = new Circle();
echo getshapename($shape); // hello
echo getshapename($circle); // world (Circle is a Shape [see liskov substitution principle])
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.