Doctrine 1.2:从模板重写 Doctrine_Record::__get()

发布于 2024-09-24 02:36:57 字数 203 浏览 4 评论 0原文

我有一个模型类(显然,它扩展了 Doctrine_Record),它“充当”自定义模板。

我想得到这样的值:“echo $record->virtual_field”。有没有办法重写自定义模板中的 getter 方法以提供自定义响应,然后将请求传递给父类或不传递?

换句话说,有没有办法从相关模板覆盖 Doctrine_Record::__get() ?

I have a model class (obviously, it extends Doctrine_Record) that "acts as" a custom template.

I want to get values like this: "echo $record->virtual_field". Is there a way to override the getter method in my custom template to provide a custom response, then either pass on the request to the parent class or not?

In other words, is there a way to override Doctrine_Record::__get() from a related template?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清引 2024-10-01 02:36:57

好的。我假设您不是在谈论实际的行为“actAs”模板。

如果定义新的 __get() 方法,它将自动覆盖父级的 __get() 方法。

现在,在新的 __get() 方法中,首先检查它是否存在于当前实例中,然后检查父实例中是否存在。

我把它放在一起(记住,现在已经是午夜了):

<?php
class bar {
public $data = array();
  public function __construct() {
    $this->data['virtual_field'] = "set in bar";
  }

  public function  __get($name) {
    if(array_key_exists($name, $this->data)) {
      return $this->data[$name];
    }
    return null;
  }

  public function __set($name, $value) {
    $this->data[$name] = $value;
  }
}

class foo extends bar {
public $data = array();
  public function __construct() {

  }

  public function  __get($name) {
    if(array_key_exists($name, $this->data)) {
      return $this->data[$name];
    }
    if (parent::__get($name))
      return parent::__get($name);

    return null;
  }

  public function __set($name, $value) {
    $this->data[$name] = $value;
  }
}

$a = new foo;
echo $a->virtual_field;

现在我不知道这对你想要实现的目标有多有效。

Ok. I assume you're not talking about actual Behaviour 'actAs' Templates.

If you define a new __get() method, it will automatically override the parent's __get() method.

Now in your new __get() method, you first check if it exists in your current instance, and then the parent's.

I hacked this together (bear in mind, it's almost midnight):

<?php
class bar {
public $data = array();
  public function __construct() {
    $this->data['virtual_field'] = "set in bar";
  }

  public function  __get($name) {
    if(array_key_exists($name, $this->data)) {
      return $this->data[$name];
    }
    return null;
  }

  public function __set($name, $value) {
    $this->data[$name] = $value;
  }
}

class foo extends bar {
public $data = array();
  public function __construct() {

  }

  public function  __get($name) {
    if(array_key_exists($name, $this->data)) {
      return $this->data[$name];
    }
    if (parent::__get($name))
      return parent::__get($name);

    return null;
  }

  public function __set($name, $value) {
    $this->data[$name] = $value;
  }
}

$a = new foo;
echo $a->virtual_field;

Now I dont know how well this works for what you are trying to achieve.

小耗子 2024-10-01 02:36:57
class Product extends Doctrine_Record {

    //...

    public function __get($name) {
        if ($name == 'virtual_field') {
            return $this->virtual_field();
        }                                                     
        else {
            return parent::__get($name);
        }
    }


    public function virtual_field() {
        // calculate or retrieve virtual field value
        return $value;
    }
}
class Product extends Doctrine_Record {

    //...

    public function __get($name) {
        if ($name == 'virtual_field') {
            return $this->virtual_field();
        }                                                     
        else {
            return parent::__get($name);
        }
    }


    public function virtual_field() {
        // calculate or retrieve virtual field value
        return $value;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文