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

trait代码复用机制问题 #688

Closed
vscing opened this issue Mar 25, 2023 · 5 comments
Closed

trait代码复用机制问题 #688

vscing opened this issue Mar 25, 2023 · 5 comments

Comments

@vscing
Copy link

vscing commented Mar 25, 2023

From manual page: https://php.net/language.oop5.traits
trait代码复用机制问题

代码正常

<?php

class A
{
    protected static $instance = null;

    public static function getInstance(...$args)
    {
        if(!isset(self::$instance)){
            self::$instance = new static(...$args);
        }
        return self::$instance;
    }

    public function getB()
    {
        return B::class::getInstance();
    }
}

class B extends A
{
    protected static $instance = null;

    public static function getInstance(...$args)
    {
        if(!isset(self::$instance)){
            self::$instance = new static(...$args);
        }
        return self::$instance;
    }
}
var_dump(A::getInstance()->getB()); // B

代码复用不正常

<?php

trait Singleton
{
    protected static $instance = null;

    public static function getInstance(...$args)
    {
        if(!isset(self::$instance)){
            self::$instance = new static(...$args);
        }
        return self::$instance;
    }
}

class A
{
    use Singleton;

    public function getB()
    {
        return B::class::getInstance();
    }
}

class B extends A
{
    use Singleton;
}
var_dump(A::getInstance()->getB()); // A
@mowangjuanzi
Copy link
Member

如果你想实现,不同的类包含不同的实例,也就是你期望的结果。

那么你只需将 trait 中的 protected static $instance = null; 修改为 private static $instance = null; 即可。

@mowangjuanzi
Copy link
Member

补充下,这个问题是属性继承问题。只有 设置为 private 的时候,每个类的属性才会不同。具体可以看下相关可访问性不同导致的变化。

@vscing
Copy link
Author

vscing commented Mar 26, 2023

@mowangjuanzi 正常的类里面,父类中的属性被子类重写了。但是用trait提取公共部分,并没有被重写。

@vscing
Copy link
Author

vscing commented Mar 26, 2023

@mowangjuanzi php/php-src#10937 辛苦看看这个是否是调整

@sy-records
Copy link
Member

Duplicate of php/php-src#10935
Fixed via php/php-src@9a250cc

这个实现会在 PHP 8.3 中包含。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants