On 26/03/2008, Richard Quadling <[EMAIL PROTECTED]> wrote:
> On 25/03/2008, Felipe Pena <[EMAIL PROTECTED]> wrote:
>
> > Em Ter, 2008-03-25 às 12:35 +0100, Lars Strojny escreveu:
>  >
>  > > Would that mean that the following code does not work anymore?
>  >  >
>  >  > <?php
>  >  > class Foo
>  >  > {
>  >  >       protected function method()
>  >  >       {
>  >  >       }
>  >  >
>  >  >       public function doSomething(Foo $foo)
>  >  >       {
>  >  >               $foo->method();
>  >  >       }
>  >  > }
>  >  >
>  >  > $foo1 = new Foo();
>  >  > $foo1->doSomething(new Foo());
>  >  >
>  >
>  >
>  > This still will works.
>
>
> Surely it shouldn't work at all unless the $foo === $this?
>
>  I understand that the checking is based upon the class and not the instance.
>
>  Shouldn't the instance be the limiting factor?
>
>  Richard.

Maybe what I am saying is a little clearer if you change protected to private.

<?php
class Foo {
    private function priv() {
        echo __METHOD__, ':', __LINE__, PHP_EOL;
    }

    protected function prot() {
        echo __METHOD__, ':', __LINE__, PHP_EOL;
    }

    public function doSomething(Foo $foo) {
        $foo->prot();
        $foo->priv();
    }
}

class Bar extends Foo {
    protected function prot() {
        echo __METHOD__, ':', __LINE__, PHP_EOL;
    }
}

$foo1 = new Foo();
$foo1->doSomething(new Bar());



outputs (PHP 5.3.0-dev (cli) (built: Mar 18 2008 04:17:56))

Bar::prot:19
Foo::priv:4

It just doesn't seem right to be able to call a private or protected
method of another instance. Sort of isn't private any more.

And as for being able to call a protected method of a completely
different class, just because it shares the same ancestry. That seems
REALLY wrong.

Richard.


-- 
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

Reply via email to