overloading methods in php 5... whats this crap?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

overloading methods in php 5... whats this crap?

Post by bg »

So I hear php5 supports overloading methods, and I think great. I really didn't think id have to learn too much, as ive overloaded methods in java and c++ and all it took was defining a method multiple times with different parameters. So I'm looking at the php documentation and Im trying to decypher their version of overloading and im thinking to myself, "What is this crap??"

Here are some examples of overloading the the php site

Example: __get() and __set()

Code: Select all

<?php
class Setter {
   public $n;
   public $x = array("a" => 1, "b" => 2, "c" => 3);

   function __get($nm) {
       print "Getting [$nm]\n";

       if (isset($this->x[$nm])) {
           $r = $this->x[$nm];
           print "Returning: $r\n";
           return $r;
       } else {
           print "Nothing!\n";
       }
   }

   function __set($nm, $val) {
       print "Setting [$nm] to $val\n";

       if (isset($this->x[$nm])) {
           $this->x[$nm] = $val;
           print "OK!\n";
       } else {
           print "Not OK!\n";
       }
   }
}

$foo = new Setter();
$foo->n = 1;
$foo->a = 100;
$foo->a++;
$foo->z++;
var_dump($foo);
?>
Example: __call()

Code: Select all

<?php
class Caller {
   private $x = array(1, 2, 3);

   function __call($m, $a) {
       print "Method $m called:\n";
       var_dump($a);
       return $this->x;
   }
}

$foo = new Caller();
$a = $foo->test(1, "2", 3.4, true);
var_dump($a);
?>
Is it me, or is it hard to follow what this code is supposed to do? I also fail to see how this is overloading.


feyd use

Code: Select all

tags for php code please.[/color]
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

I wouldn't call this overloading. But this approach could be used to emulate overloading (and properties like in BCB). It just 'routes' all gets, sets and method calls via common functions, namely __get, __set and __call.
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Post by bg »

I suppose, but you could already emulate overloading using func_get_args(). I guess im confused as to why they'd just create another workaround for overloading rather than just actually implement it. Kinda dissapointing.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

__call can be used to handle undefined functions too. There are useful things to play with using __get and __set as well.
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Post by bg »

Weirdan wrote:__call can be used to handle undefined functions too. There are useful things to play with using __get and __set as well.
Ok, I'm starting to see some usefulness, but got a couple more questions.

Why would you make a call to an undefined function within an object?

As far as get/set are concerned, they only seem to be useful when using them on a single array within the class, as php.net does in all their examples.

IMO, being able to use multiple __get,__set methods (i dont know if they would technically be methods anymore) for private or protected members of a class would be much more useful.

For example something like this:

Code: Select all

class myClass{

private $foo = 1;
private $bar = 2;

__get foo{

return $this->foo;

}

__set foo($val){

$this->foo = $val;

}

__get foobar{

return ($this->foo + $this->bar);

}
}

$test = new myClass();

$test->foo = 10;

echo $test->foo;

echo $test->foobar;
ps - might wanna update the php code formatting to include new php5 syntax. Hopefull jEdit will soon :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

let's consider this class:

Code: Select all

class Object {
  public function __call($method, $args) {
     if( !method_exists($this, $method) ) {
          echo "<pre>";
          echo "Call to undefined method: $method";
          debug_print_backtrace();
          echo "</pre>";
          return false;
     } else {
          return call_user_func_array(array(&$this, $method), $args);
     }
  }
  public __get($prop_name, &$prop_value) {
      if( method_exists($this, '__get_' . $prop_name) {
          $prop_value = call_user_func(array(&$this, '__get_' . $prop_name));
      } elseif( isset($this->$prop_name) ) {
          $prop_value = $this->$prop_name;
      } else {
          echo "<pre>";
          echo "Attempt to access to undefined property: $prop_name";
          debug_print_backtrace();
          echo "</pre>";
          return false;
      }
   }
}
class someclass extends Object {
   public function __get_asd() {
       return "something";
   }
}
$a = new someclass();
$a->undefined_function();
echo $a->asd;
it would provide you with call stack backtrace whenever some undefined function in someclass is called.
Post Reply