Overriding a magic method in PHP

I searched Google for an hour and found variants of this problem, 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 has no such method, but implements __call()

to expose this functionality.

For example:

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

      

This does not work. I am actually getting a nasty internal Apache error or misconfiguration.

I found this solution, which doesn't work either: (Except I added a part array($new_id)

. The example uses NULL.)

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

      

It should have worked! I don't know what I am doing wrong.

What is the correct way to do this?

Edit:

I'm not sure what happened to my config, but the error messages I get here are simply:

[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 lacks the code I'm trying to call.

In fact, this is the Base class of the Doctrine 1 class called "BaseCategory", which extends from sfDoctrineRecord.

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

      

The code for the sfDoctrineRecord class is here:

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

+3


source to share


1 answer


As moonwave99 suggested you should take a look at here

Try to use parent :: method to not declare a method using __call, because in this case the string method is 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 could be the reason for your error, so when you do parent::setParentId($new_id);

, it is likely that it is being interpreted as:

parent :: set p Arent I d ($ new_id);

Try using lowercase letters in class A

+1


source







All Articles