# Confusing performance of the magic method __call

- Thread: https://externals.io/message/49193
- Original archive: https://news-web.php.net/php.internals/49193
- Started by Shijiang on 2010-08-03T11:09:46+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #49193 — Shijiang — 2010-08-03T11:09:46+00:00

URL: https://externals.io/message/49193#49193

Hi,

In the following sample code, I expected that the magic method __call only works within the class Test.
But it seems that the access control keyword private lost its efficacy when working together with the magic method __call.

<?php
class Test{
    private function __call($name,$params){   
        echo $name,"\n";
        echo $params[0];
    }   

    public function bar(){   
        $this->kakaka('afaafaf');
    }   
}
$foo=new Test;
$foo->bar('sfsfss');
$foo->nothing('works'); // this also works without any errors.
?>

IMHO, since the function __call is a method of a class, it should obey the visibility rules.

Cheers.

---

## #49194 — Ferenc Kovacs — 2010-08-03T11:14:37+00:00

URL: https://externals.io/message/49193#49194 · In reply to: #49193

2010/8/3 Shijiang <shijiang@staff.sina.com.cn>:
> Hi,
>
> In the following sample code, I expected that the magic method __call only works within the class Test.
> But it seems that the access control keyword private lost its efficacy when working together with the magic method __call.
>
> <?php
> class Test{
>    private function __call($name,$params){
>        echo $name,"\n";
>        echo $params[0];
>    }
>
>    public function bar(){
>        $this->kakaka('afaafaf');
>    }
> }
> $foo=new Test;
> $foo->bar('sfsfss');
> $foo->nothing('works'); // this also works without any errors.
> ?>
>
> IMHO, since the function __call is a method of a class, it should obey the visibility rules.
>
> Cheers.
>



http://www.php.net/manual/en/migration53.incompatible.php

The magic methods __get, __set, __isset, __unset, and __call must
always be public and can no longer be static. Method signatures are
now enforced.

Tyrael

---

## #49195 — Victor Bolshov — 2010-08-03T11:15:35+00:00

URL: https://externals.io/message/49193#49195 · In reply to: #49193

Shijiang, did you notice the

--
Warning: The magic method __call() must have public visibility and cannot be
static in Command line code on line 1
--

???

2010/8/3 Shijiang <shijiang@staff.sina.com.cn>

> Hi,
>
> In the following sample code, I expected that the magic method __call only
> works within the class Test.
> But it seems that the access control keyword private lost its efficacy when
> working together with the magic method __call.
>
> <?php
> class Test{
>    private function __call($name,$params){
>        echo $name,"\n";
>        echo $params[0];
>    }
>
>    public function bar(){
>        $this->kakaka('afaafaf');
>    }
> }
> $foo=new Test;
> $foo->bar('sfsfss');
> $foo->nothing('works'); // this also works without any errors.
> ?>
>
> IMHO, since the function __call is a method of a class, it should obey the
> visibility rules.
>
> Cheers.
>



-- 
С уважением,
Виктор

---

## #49196 — Ferenc Kovacs — 2010-08-03T11:23:47+00:00

URL: https://externals.io/message/49193#49196 · In reply to: #49195

2010/8/3 Victor Bolshov <crocodile2u@gmail.com>:
> Shijiang, did you notice the
>
> --
> Warning: The magic method __call() must have public visibility and cannot be
> static in Command line code on line 1
> --
>
> ???
>
> 2010/8/3 Shijiang <shijiang@staff.sina.com.cn>
>
> > […]
>
>
>
> --
> С уважением,
> Виктор
>

It was added/changed with 5.3 so it is not necessarly obvious.

Tyrael

---

## #49197 — Victor Bolshov — 2010-08-03T11:54:54+00:00

URL: https://externals.io/message/49193#49197 · In reply to: #49196

Yes, sorry. I've been working with 5.3 for pretty long time so I found the
original letter a bit strange.

2010/8/3 Ferenc Kovacs <info@tyrael.hu>

> 2010/8/3 Victor Bolshov <crocodile2u@gmail.com>:
> > […]
> be
> > […]
> only
> > […]
> when
> > […]
> the
> > […]
>
> It was added/changed with 5.3 so it is not necessarly obvious.
>
> Tyrael
>



-- 
С уважением,
Виктор

---

## #49201 — Shijiang — 2010-08-04T02:21:20+00:00

URL: https://externals.io/message/49193#49201 · In reply to: #49195

I was working with PHP 5.2.9 with error_report=E_ALL and display_errors=On and got no error at all.

Just now I tried it under PHP 5.3.0 and got the warning.

Sometimes although I declared the magic method __call(), I want to prevent it from being called outside the class. 
Could someone be kindly enough to tell me why it must be  public?

Thanks.
  ----- Original Message ----- 
  From: Victor Bolshov 
  To: Shijiang 
  Cc: PHP internals 
  Sent: Tuesday, August 03, 2010 7:15 PM
  Subject: Re: [PHP-DEV] Confusing performance of the magic method __call


  Shijiang, did you notice the


  --
  Warning: The magic method __call() must have public visibility and cannot be static in Command line code on line 1
  --


  ???


  2010/8/3 Shijiang <shijiang@staff.sina.com.cn>

    Hi,

    In the following sample code, I expected that the magic method __call only works within the class Test.
    But it seems that the access control keyword private lost its efficacy when working together with the magic method __call.

    <?php
    class Test{
       private function __call($name,$params){
           echo $name,"\n";
           echo $params[0];
       }

       public function bar(){
           $this->kakaka('afaafaf');
       }
    }
    $foo=new Test;
    $foo->bar('sfsfss');
    $foo->nothing('works'); // this also works without any errors.
    ?>

    IMHO, since the function __call is a method of a class, it should obey the visibility rules.

    Cheers.




  -- 
  С уважением,
  Виктор

---

## #49202 — Guillaume Rossolini — 2010-08-04T02:50:33+00:00

URL: https://externals.io/message/49193#49202 · In reply to: #49201

2010/8/4 Shijiang <shijiang@staff.sina.com.cn>

> I was working with PHP 5.2.9 with error_report=E_ALL and display_errors=On
> and got no error at all.
>
> Just now I tried it under PHP 5.3.0 and got the warning.
>
> Sometimes although I declared the magic method __call(), I want to prevent
> it from being called outside the class.
> Could someone be kindly enough to tell me why it must be  public?
>
> Thanks.
>
>
Hi,

To get the error messages mentioned above, you might want to try the
following error reporting level: `E_ALL` | `E_STRICT` 
Please refer to the manual to understand why `E_ALL` does not actually mean
all error levels:
http://www.php.net/manual/en/function.error-reporting.php (below "Notes")

Also, your php.ini file for PHP 5.3 has details explaining why this version
includes `E_STRICT` (I am guessing you chose the "development" version).

As for your other question regarding why the magic method visibility works
as it does, the answer has been given in the following bug reports:
http://bugs.php.net/bug.php?id=40056
http://bugs.php.net/bug.php?id=44769

Regards,

--
Guillaume Rossolini
