Private methods can't be called by owning class from parent #251

Open
damienalexandre opened this Issue Mar 4, 2014 · 7 comments

Projects

None yet

4 participants

@damienalexandre
damienalexandre commented Mar 4, 2014 edited

Hi there :neckbeard:

I'm trying to port this PHP code to Zephir:

<?php

class Foo extends Bar {
    public function run() {
        $this->runBar();
    }
}

class Bar {
    public function runBar() {
        $this->isPrivate();
    }

    private function isPrivate() {
        echo "YEAH";
    }
}

$f = new Foo(); 
$f->run();

So here is my Zephir code:

namespace Jolicoucou;

class Foo extends \Jolicoucou\Bar
{
    public function run()
    {
      this->runBar();
    }
}
namespace Jolicoucou;

class Bar
{
    public function runBar()
    {
      this->isPrivate();
    }

    private function isPrivate()
    {
      echo "IS NEVER CALLED";
    }
}

And the error I get:

php > $f = new Jolicoucou\Foo(); $f->run();
PHP Warning:  Invalid callback Jolicoucou\Foo::isprivate, cannot access private method Jolicoucou\Foo::isPrivate() in php shell code on line 1
PHP Stack trace:
PHP   1. {main}() php shell code:0
PHP   2. Jolicoucou\Foo->run() php shell code:1
PHP   3. Jolicoucou\Bar->runBar() php shell code:1
PHP Fatal error:  Call to undefined function Jolicoucou\Foo::isprivate() in php shell code on line 1
PHP Stack trace:
PHP   1. {main}() php shell code:0
PHP   2. Jolicoucou\Foo->run() php shell code:1
PHP   3. Jolicoucou\Bar->runBar() php shell code:1

A class owning a private method can't call this method if we use a child class? Running $f = new Jolicoucou\Bar(); $f->runBar(); works fine.

Thanks a lot,
🍺 Damien

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/3677903-private-methods-can-t-be-called-by-owning-class-from-parent?utm_campaign=plugin&utm_content=tracker%2F280146&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F280146&utm_medium=issues&utm_source=github).
@damienalexandre damienalexandre added a commit to jolicode/symfony2-eventdispatcher-extension that referenced this issue Mar 8, 2014
@damienalexandre damienalexandre Use protected instead of private (see phalcon/zephir#251) 818d037
@nkt nkt added the bug label Mar 18, 2014
@ovr
Contributor
ovr commented Mar 19, 2014

private can`t be called in php, tested with @nkt. Thx our super amazing Collaborator @nkt. <3

@nkt
Contributor
nkt commented Mar 19, 2014

@ovr right. That is not zephir mistake - it is how php works. Try to run this code in php:

<?php

class Foo
{
    public function bar()
    {
        $this->foo();
    }

    private function foo()
    {
        echo 1;
    }
}

class Bar extends Foo
{
    public function test()
    {
        $this->bar();
    }
}

$b = new Bar;
$b->test();
@nkt nkt closed this Mar 19, 2014
@nkt nkt removed the bug label Mar 19, 2014
@damienalexandre

My PHP example is running, but your's is not; maybe I need a coffee but I do not see what is the difference between the two PHP examples :neckbeard:

@ovr
Contributor
ovr commented Mar 19, 2014

@damienalexandre because we can't use foo name for name.
This caused because foo method would be a construct for class

@damienalexandre

Ah yes didn't see that thank.

My issue is not about trying to call a constructor. Renaming foo to toto make this code works in PHP:

<?php

class Foo
{
    public function bar()
    {
        $this->toto();
    }

    private function toto()
    {
        echo 1;
    }
}

class Bar extends Foo
{
    public function test()
    {
        $this->bar();
    }
}

$b = new Bar;
$b->test();

But Zephir is handling private method differently and we can't implement this example with it.

@nkt
Contributor
nkt commented Mar 19, 2014

Yes. You are right.

@nkt nkt reopened this Mar 19, 2014
@nkt nkt added the bug label Mar 19, 2014
@fezfez
Contributor
fezfez commented Nov 4, 2014

Hi,

I'm facing with the same issue, could you try to fix it ?

Thanks for your awesome work !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment