Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
damienalexandre opened this issue Mar 4, 2014 · 11 comments

Comments

@damienalexandre
Copy link

commented Mar 4, 2014

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

damienalexandre added a commit to jolicode/symfony2-eventdispatcher-extension that referenced this issue Mar 8, 2014

@nkt nkt added the bug label Mar 18, 2014

@ovr

This comment has been minimized.

Copy link
Contributor

commented Mar 19, 2014

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

@nkt

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Author

commented Mar 19, 2014

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Author

commented Mar 19, 2014

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor

commented Nov 4, 2014

Hi,

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

Thanks for your awesome work !

sjinks added a commit to sjinks/zephir that referenced this issue Apr 2, 2017

@sjinks

This comment has been minimized.

Copy link
Contributor

commented Apr 2, 2017

Right now only PHP 5.4, 5.5, and 7.0 is affected

sjinks added a commit to sjinks/zephir that referenced this issue Apr 2, 2017

sjinks added a commit to sjinks/zephir that referenced this issue Apr 2, 2017

sjinks added a commit to sjinks/zephir that referenced this issue Apr 2, 2017

sjinks added a commit to sjinks/zephir that referenced this issue Apr 9, 2017

sjinks added a commit to sjinks/zephir that referenced this issue Apr 9, 2017

sjinks added a commit to sjinks/zephir that referenced this issue Apr 9, 2017

sjinks added a commit to sjinks/zephir that referenced this issue Apr 9, 2017

@pentagonal

This comment has been minimized.

Copy link

commented Nov 25, 2017

PHP-7.0

Test using Pimple\Container implements ArrayAccess
Extended by Slim Container class. (Affected By Magic Method)
image

image

Looks like the the code has jumping...
The magic method called first than constructor.
This is affected when the parent constructor contains private properties...

@sergeyklay

This comment has been minimized.

Copy link
Member

commented Apr 24, 2018

@pentagonal Could you please open a new issues with a code to reproduce your issue.

@sergeyklay

This comment has been minimized.

Copy link
Member

commented Apr 24, 2018

Calling parent's private methods from the child's public ones doesn't provided for PHP < 5.6. This feature works well now for PHP 5.6, 7.0, 7.1, 7.2 and even for 7.3 (nightly build) For older PHP versions like PHP 5.5 this will not be implemented. I want to close this issue because I don't want a bunch of these lying around. I'd like to add support for anything/everything at some point, but keeping the issue open doesn't help that. :) If anyone wants to get started, I'd love that.

Refs:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.