Sebastián Grignoli
Sebastián Grignoli

Reputation: 33432

Overriding a magic method in PHP

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.

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.

I've found this solution that didn't work either: (Except I added the array($new_id) part. The example used 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.

In fact, it's a Doctrine 1 model "Base" class called "BaseCategory" that extends from 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:

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

Upvotes: 4

Views: 1176

Answers (1)

Sid
Sid

Reputation: 14896

As moonwave99 suggested, you should have a look here

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

<?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);

Try to use lowercase as well in class A

Upvotes: 2

Related Questions