[PHP]About the magic function __call

2008-08-27 Thread Paulo Sousa
Hi there!

I'm working with the following code:

?php

abstract class Foo{

protected $a;
protected $b;
protected $c;

 function __construct($arg){
 $this-a = $arg;
 }

 function __call($function, $args){
 $this-b = $function;
 $this-c = $args;
 $this-doWhatever();
 }

 private doWhatever(){
 }
}


class Boo extends Foo{

protected $e;

public function __construct(){
 parent::__construct('Blah');
 }

public function drive(){
  $e = 'testing';
  parent::drive($e);
 }
}


$br = new Boo();
$br-drive();



But I get a Fatal error: Call to undefined method Foo::drive()

The magic fuction __call don't catch the drive(). Why not?

I need another idea for this problem and avoid edit the abstract class.

Sorry about the english.

Thanks for any help


Re: [PHP]About the magic function __call

2008-08-27 Thread Nathan Nobbe
On Wed, Aug 27, 2008 at 1:35 PM, Paulo Sousa [EMAIL PROTECTED]wrote:

 Hi there!

 I'm working with the following code:

 ?php

 abstract class Foo{

 protected $a;
 protected $b;
 protected $c;

  function __construct($arg){
  $this-a = $arg;
  }

  function __call($function, $args){
  $this-b = $function;
  $this-c = $args;
  $this-doWhatever();
  }

  private doWhatever(){
  }
 }


 class Boo extends Foo{

 protected $e;

 public function __construct(){
  parent::__construct('Blah');
  }

 public function drive(){
  $e = 'testing';
  parent::drive($e);
  }
 }


 $br = new Boo();
 $br-drive();



 But I get a Fatal error: Call to undefined method Foo::drive()

 The magic fuction __call don't catch the drive(). Why not?

 I need another idea for this problem and avoid edit the abstract class.


looks like __call() might not work through a subclass if defined in the
parent; i might poke around in the .phpt tests that come w/ the php source
to ensure this is the correct behavior.

in the meantime you can get away w/ some variant this ugliness,

class B extends A {
function __call($method, $args) {
return parent::__call($method, $args);
}
}

-nathan


Re: [PHP]About the magic function __call

2008-08-27 Thread Nathan Nobbe
On Wed, Aug 27, 2008 at 1:49 PM, Nathan Nobbe [EMAIL PROTECTED]wrote:

 On Wed, Aug 27, 2008 at 1:35 PM, Paulo Sousa [EMAIL PROTECTED]wrote:
  ...


this *should* work,

here is a test, tests/classes/__call_005.phpt, you can take the part beneath
the --FILE-- section and see if it blows up on your system or not.
right now, im getting errors on a php5.2.5 system, and its working as
expected on a php5.2.6 system.

--TEST--
When __call() is invoked via ::, ensure private implementation of __call()
in superclass is accessed without error.
--FILE--
?php
class A {
private function __call($strMethod, $arrArgs) {
echo In  . __METHOD__ . ($strMethod, array( .
implode(',',$arrArgs) . ))\n;
var_dump($this);
}
}

class B extends A {
function test() {
A::test1(1,'a');
B::test2(1,'a');
self::test3(1,'a');
parent::test4(1,'a');
}
}

$b = new B();
$b-test();
?
--EXPECTF--
In A::__call(test1, array(1,a))
object(B)#1 (0) {
}
In A::__call(test2, array(1,a))
object(B)#1 (0) {
}
In A::__call(test3, array(1,a))
object(B)#1 (0) {
}
In A::__call(test4, array(1,a))
object(B)#1 (0) {
}

also, i found in the code you posted, you are missing a 'function' in front
of doWhatever (cause a parse error, which is why i mention it); w/ the
following modification to your code, its running fine on a php 5.2.6 system,
and choking on 5.2.5;

  function __call($function, $args){
var_dump($function);
var_dump($args);
  $this-doWhatever();
  }

   private function doWhatever() {
}

[EMAIL PROTECTED] ~/unpack/php-5.2.6RC3/tests/classes $ php testOtherStuff.php
string(5) drive
array(1) {
  [0]=
  string(7) testing
}

-nathan

(sorry for the long-winded post)


Re: [PHP]About the magic function __call

2008-08-27 Thread Paulo Sousa
My information was incomplete: I'm running php 5.1.2 (a requirement from the
customer).
I found this http://bugs.php.net/bug.php?id=42937

I coded this without testing, using only the idea. Thanks!

Thanks for the help Nathan!


2008/8/27 Nathan Nobbe [EMAIL PROTECTED]

 On Wed, Aug 27, 2008 at 1:49 PM, Nathan Nobbe [EMAIL PROTECTED]wrote:

 On Wed, Aug 27, 2008 at 1:35 PM, Paulo Sousa [EMAIL PROTECTED]
  wrote:
  ...


 this *should* work,

 here is a test, tests/classes/__call_005.phpt, you can take the part
 beneath the --FILE-- section and see if it blows up on your system or not.
 right now, im getting errors on a php5.2.5 system, and its working as
 expected on a php5.2.6 system.

 --TEST--
 When __call() is invoked via ::, ensure private implementation of __call()
 in superclass is accessed without error.
 --FILE--
 ?php
 class A {
 private function __call($strMethod, $arrArgs) {
 echo In  . __METHOD__ . ($strMethod, array( .
 implode(',',$arrArgs) . ))\n;
 var_dump($this);
 }
 }

 class B extends A {
 function test() {
 A::test1(1,'a');
 B::test2(1,'a');
 self::test3(1,'a');
 parent::test4(1,'a');
 }
 }

 $b = new B();
 $b-test();
 ?
 --EXPECTF--
 In A::__call(test1, array(1,a))
 object(B)#1 (0) {
 }
 In A::__call(test2, array(1,a))
 object(B)#1 (0) {
 }
 In A::__call(test3, array(1,a))
 object(B)#1 (0) {
 }
 In A::__call(test4, array(1,a))
 object(B)#1 (0) {
 }

 also, i found in the code you posted, you are missing a 'function' in front
 of doWhatever (cause a parse error, which is why i mention it); w/ the
 following modification to your code, its running fine on a php 5.2.6 system,
 and choking on 5.2.5;

   function __call($function, $args){
 var_dump($function);
 var_dump($args);
   $this-doWhatever();
   }

private function doWhatever() {
 }

 [EMAIL PROTECTED] ~/unpack/php-5.2.6RC3/tests/classes $ php testOtherStuff.php
 string(5) drive
 array(1) {
   [0]=
   string(7) testing
 }

 -nathan

 (sorry for the long-winded post)