覆盖PHP中的魔术方法

时间:2023-01-15 20:44:09

I've googled for an hour and found variants of this issue but not this exact version of the problem, except for one blog post that didn't work.

我用谷歌搜索了一个小时,发现了这个问题的变种,但没有找到问题的确切版本,除了一篇不起作用的博客文章。

I want to override a method in a PHP extended class, and call the parent inside the body of the new one. The problem is that the parent does not have such method, but implements __call() to expose that functionality.

我想覆盖PHP扩展类中的方法,并调用新主体内部的父级。问题是父级没有这样的方法,但实现了__call()来公开该功能。

For example:

class B extends A {
  public function setParentId($new_id) 
  {
     parent::setParentId($new_id);
     $this->buildParentTreeCache();
     return $this;
  }

This doesn't work. In fact I get a nasty Apache internal error or misconfiguration.

这不起作用。事实上,我得到了令人讨厌的Apache内部错误或配置错误。

I've found this solution that didn't work either: (Except I added the array($new_id) part. The example used NULL.)

我发现这个解决方案也不起作用:(除非我添加了数组($ new_id)部分。该示例使用NULL。)

class B extends A {
  public function setParentId($new_id) 
  {
     parent::__call('setParentId', array($new_id));
     $this->buildParentTreeCache();
     return $this;
  }

This one should have worked! I don't know what I'm doing wrong.

这应该有用!我不知道我做错了什么。

What's the right way to do this?

这样做的正确方法是什么?

Edit:

I'm not sure what's wrong with my configuration, but the error messages I 'm getting here are just:

我不确定我的配置有什么问题,但我在这里收到的错误信息只是:

[Mon May 04 12:13:12.778136 2015] [fastcgi:error] [pid 30512] (104)Connection reset by peer: [client 127.0.0.1:54854] FastCGI: comm with server "/var/www/fastcgi/php5.fastcgi" aborted: read failed, referer: http://admin.mysite/index.php/category/index
[Mon May 04 12:13:12.779620 2015] [fastcgi:error] [pid 30512] [client 127.0.0.1:54854] FastCGI: incomplete headers (0 bytes) received from server "/var/www/fastcgi/php5.fastcgi", referer: http://admin.mysite/index.php/category/index

Edit 2:

The base class (Class A in this example) is an autogenerated file that also does not have the code I'm trying to call.

基类(本例中为A类)是一个自动生成的文件,它也没有我试图调用的代码。

In fact, it's a Doctrine 1 model "Base" class called "BaseCategory" that extends from sfDoctrineRecord.

实际上,它是一个名为“BaseCategory”的Doctrine 1模型“Base”类,它从sfDoctrineRecord扩展而来。

class Category extends BaseCategory // My class
class BaseCategory extends sfDoctrineRecord // Autogenerated, prone to be verwritten by the generator

The code for class sfDoctrineRecord is here:

类sfDoctrineRecord的代码在这里:

http://trac.symfony-project.org/browser/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php

1 个解决方案

#1


As moonwave99 suggested, you should have a look here

正如moonwave99建议的那样,你应该看看这里

Take care of using parent::method with not declared method when using __call because in this case the string method will be lowercase:

在使用__call时,请注意使用未声明方法的parent ::方法,因为在这种情况下,字符串方法将为小写:

<?php
  class A {
      function __call( $method, $args ) {
        echo get_class( $this )  . ' ' . $method . "\n";
      }
  }

  class B extends A {
      function getTest() {
          parent::getTest();
      }
  }

$a = new A();
$a->getTest();
$b = new B();
$b->getTest();

?>

output:

A getTest
B gettest

This might be what is causing your error, so when you are doing parent::setParentId($new_id); it is likely that it is interpreteded as:

这可能是导致错误的原因,所以当你在做parent :: setParentId($ new_id)时;它很可能被解释为:

parent::setparentid($new_id);

Try to use lowercase as well in class A

尝试在A类中使用小写

#1


As moonwave99 suggested, you should have a look here

正如moonwave99建议的那样,你应该看看这里

Take care of using parent::method with not declared method when using __call because in this case the string method will be lowercase:

在使用__call时,请注意使用未声明方法的parent ::方法,因为在这种情况下,字符串方法将为小写:

<?php
  class A {
      function __call( $method, $args ) {
        echo get_class( $this )  . ' ' . $method . "\n";
      }
  }

  class B extends A {
      function getTest() {
          parent::getTest();
      }
  }

$a = new A();
$a->getTest();
$b = new B();
$b->getTest();

?>

output:

A getTest
B gettest

This might be what is causing your error, so when you are doing parent::setParentId($new_id); it is likely that it is interpreteded as:

这可能是导致错误的原因,所以当你在做parent :: setParentId($ new_id)时;它很可能被解释为:

parent::setparentid($new_id);

Try to use lowercase as well in class A

尝试在A类中使用小写