【问题标题】:What is the most efficient way to get property values from parent object? [closed]从父对象获取属性值的最有效方法是什么? [关闭]
【发布时间】:2013-01-29 09:09:01
【问题描述】:

我有一个单实例父类,它包含“本地全局属性”,每个子类都应该有权访问(在更改时也是如此)

我可以想到两种方法来确保来自实例化父级的子级继承父级值:

1) 使用静态变量

class p
{
public static $var = 0;
}

class c extends p
{
function foo()
{
echo parent::$var;
}
}

$p = new p;
$c = new c;

$c->foo(); //echoes 0
p::$var = 2;
$c->foo(); //echoes 2

2) 使用传递的对象

    class p
    {
          public $var = 0;
    }

    class c extends p
    {
           function update_var(p $p)
      {
            $this->var = $p->var;
      }

    function foo()
    {
    echo $this->var;
    }
    }

    $p = new p;
    $c = new c;

    $c->foo(); //echoes 0
    $p->var = 2;
$c->update_var($p);
    $c->foo(); //echoes 2

在我看来,静态解决方案是迄今为止最优雅的解决方案,但可能存在一些我没有看到的缺点。您认为哪种解决方案是最好的,还是有第三种更好的选择?

(另请注意,在此示例中 $var 是公开的,以使此示例更易于说明,但最终会受到保护)

【问题讨论】:

    标签: php class inheritance


    【解决方案1】:

    每个子类都应该有权访问(在更改时也是如此)

    这意味着您要使用静态变量,因为这正是它们的设计目的。

    另一种选择是使用常量,但如果它们完全适合您的班级,那么static 就是要走的路

    编辑:或者,如果您需要更复杂的东西,您可以使用@true 建议的模式。

    【讨论】:

    • 非常感谢。这也是我希望是正确的解决方案:)
    【解决方案2】:

    也许最好有不同的对象作为这些变量的存储,可以注入到每个对象中?

    更新变量的想法似乎很疯狂。

    如果你需要更少的代码,你可以使用静态变量来完成,但我会做一些配置容器,它将被注入到每个类中(阅读依赖注入:http://en.wikipedia.org/wiki/Dependency_injection

    class Config
    {
        protected $vars;
    
        public function __get($name)
        {
            if (isset($this->vars[$name])) {
                return $this->$name;
            }
        }
    
        public function __set($name, $value)
        {
            return $this->vars[$name] = $value;
        }
    
        public function toArray()
        {
            return $this->vars;
        }
    }
    
    class Component 
    {
        protected $config;
    
        public function __construct(Config $config = null)
        {
            $this->config = $config;
        }
    
        public function getConfig()
        {
            return $this->config;
        }
    }
    
    // Create config instance
    $config = new Config();
    
    // Store variable
    $config->testVar = 'test value';
    
    // Create component and inject config
    $component = new Component($config);
    
    // Save another one variable in config
    $config->test2Var = 'test 2 value';
    
    // Create one more component and inject config
    $component2  = new Component($config);
    
    // Create one more varible
    $config->someMoreVar = 'another one variable';
    
    // Check configs in different components, values will be same (because config object is not cloned, just link to this object is saved in Component's $config property):
    var_dump($component->getConfig()->toArray());
    var_dump($component2->getConfig()->toArray());
    

    附:我没有测试过这段代码,只是为了展示一个想法

    【讨论】:

    • 你会用它来存储所有父类的所有配置值吗?或者为每个父类创建一个配置类?我有几个父类,每个都有多个“全局”属性要传递,父类实际上不应该访问其他父类的配置属性。
    • 但我确实喜欢将所有配置值放在同一个地方的想法。也许我可以进行某种身份验证,以确保正确的类只能访问正确的属性...
    • 目前在我的项目中,我使用 DI 和 __get __set 方法,所以我可以使用 $this->config 调用从任何对象访问配置。
    • 我的意思是,我可能不希望任何对象访问所有配置属性。但是感谢您提供替代解决方案,在解决之前我需要多考虑一下。
    猜你喜欢
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2020-05-26
    • 2021-08-03
    • 1970-01-01
    • 2021-10-01
    相关资源
    最近更新 更多